Advertisement
csaki

órai feladat

Mar 6th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  telefon
  4. //
  5. //  Created by Admin on 2014.03.06..
  6. //  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12.  
  13. @end
  14.  
  15. @implementation ViewController
  16.  
  17. - (void)viewDidLoad
  18. {
  19.     [super viewDidLoad];
  20.     NSString *path = [[NSBundle mainBundle] pathForResource:@"phones" ofType:@"plist"];
  21.     allUser=[[NSArray alloc] initWithContentsOfFile:path];
  22.     searchedUser = [[NSMutableArray alloc] init];
  23.         NSLog(@"%@", allUser);
  24.    
  25.     UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 150, 30)];
  26.     field.Tag=1000;
  27.     field.borderStyle=UITextBorderStyleRoundedRect;
  28.     field.autocorrectionType=UITextAutocorrectionTypeNo;
  29.     field.delegate=self;
  30.     field.autocapitalizationType=UITextAutocapitalizationTypeNone;
  31.     [self.view addSubview:field];
  32.    
  33.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  34.     button.frame=CGRectMake(170, 10, 50, 30);
  35.     [button setTitle:@"Keres" forState:UIControlStateNormal];
  36.     [button addTarget:self action:@selector(searchButtonClicked:) forControlEvents:UIControlEventTouchDown];
  37.     [self.view addSubview:button];
  38.    
  39.     UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(10, 50, self.view.frame.size.width-20, self.view.frame.size.height-60)];
  40.     tempView.tag=1001;
  41.     tempView.backgroundColor=[UIColor grayColor];
  42.     tempView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  43.     [self.view addSubview:tempView];
  44. }
  45.  
  46. -(BOOL)textFieldShouldReturn:(UITextField *)textField
  47. {
  48.    
  49.     [self searchButtonClicked:NULL];
  50.     [textField resignFirstResponder];
  51.     return true;
  52. }
  53.  
  54.  
  55. -(void)searchButtonClicked:(UIButton *)sender
  56. {
  57.     NSString *searched = ((UITextField*)[self.view viewWithTag:1000]).text;
  58.     searched=[searched stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  59.     if (searched.length>0)
  60.     {
  61.         for (NSDictionary *tempDict in allUser)
  62.         {
  63.             NSString *tempname=(NSString *)[tempDict objectForKey:@"name"];
  64.             NSRange range = [tempname rangeOfString:searched options:NSCaseInsensitiveSearch];
  65.             if (range.length > 0)
  66.             {
  67.                 [searchedUser addObject:tempDict];
  68.             }  
  69.         }
  70.     }
  71.     [self showSearchedUser];
  72. }
  73.  
  74. -(void)showUserActionSheet:(UIButton *)sender
  75. {
  76.     clickedUser=[searchedUser objectAtIndex:sender.tag];
  77.     UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:[[searchedUser objectAtIndex:sender.tag] objectForKey:@"name"] delegate:self cancelButtonTitle:@"Vissza" destructiveButtonTitle:@"Törlés" otherButtonTitles:@"Mutat", nil];
  78.     [sheet showInView:self.view];
  79. }
  80.  
  81. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  82. {
  83.     switch (buttonIndex) {
  84.         case 0:
  85.         {
  86.             [searchedUser removeObject:clickedUser];
  87.             [self showSearchedUser];
  88.         }
  89.             break;
  90.         case 1:
  91.         {
  92.  
  93.         }
  94.             break;
  95.         default:
  96.             break;
  97.     }
  98.        
  99. }
  100.  
  101. -(void)showSearchedUser
  102. {  
  103.     UIView *view = [self.view viewWithTag:1001];
  104.     for (UIView *tempbutton in [view subviews])
  105.     {
  106.         [tempbutton removeFromSuperview];
  107.     }
  108.     int i = 0;
  109.     for (NSDictionary *dict in searchedUser)
  110.     {
  111.         UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  112.         int buttonHeight = 20;
  113.         button.frame=CGRectMake(10, i++ * buttonHeight, view.frame.size.width-10, buttonHeight);
  114.         [button setTitle:[dict objectForKey:@"name"] forState:UIControlStateNormal];
  115.         [button addTarget:self action:@selector(showUserActionSheet:) forControlEvents:UIControlEventTouchDown];
  116.         button.tag = [searchedUser indexOfObject:dict];
  117.         [view addSubview:button];
  118.     }  
  119. }
  120.  
  121. - (void)viewDidUnload
  122. {
  123.     [super viewDidUnload];
  124.     // Release any retained subviews of the main view.
  125. }
  126.  
  127. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  128. {
  129.     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
  130.         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  131.     } else {
  132.         return YES;
  133.     }
  134. }
  135.  
  136. @end
  137.  
  138.  
  139.  
  140.  
  141.  
  142. .h:
  143.  
  144. #import <UIKit/UIKit.h>
  145.  
  146. @interface ViewController : UIViewController <UITextFieldDelegate, UIActionSheetDelegate>
  147. {
  148.     NSArray *allUser;
  149.     NSMutableArray *searchedUser;
  150.     NSDictionary *clickedUser;
  151. }
  152.  
  153. -(void)searchButtonClicked:(UIButton *)sender;
  154. -(void)showSearchedUser;
  155. -(void)showUserActionSheet:(UIButton *)sender;
  156. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement