Guest User

.m

a guest
Jan 27th, 2015
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "BNRAppDelegate.h"
  2.  
  3. @implementation BNRAppDelegate
  4.  
  5. #pragma mark - applciation delegate callbacks.
  6.  
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  8. {
  9.     // Create empty array      
  10.     self.tasks = [[NSMutableArray alloc] init];
  11.    
  12.     // Create and configure the UIWindow instance.
  13.     // a CGRect is a struct with an origin (x,y) and a size (width, height)
  14.     CGRect winFrame = [[UIScreen mainScreen] bounds];
  15.     UIWindow *theWindow = [[UIWindow alloc] initWithFrame:winFrame];
  16.     self.window = theWindow;
  17.    
  18.     CGRect tableFrame = CGRectMake(0, 80, winFrame.size.width, winFrame.size.height - 100);
  19.     CGRect fieldFrame = CGRectMake(20, 40, 200, 31);
  20.     CGRect buttonFrame = CGRectMake(228, 40, 72, 31);
  21.    
  22.     // Create and configure table view
  23.     self.taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
  24.     self.taskTable.separatorStyle = UITableViewCellSeparatorStyleNone;
  25.     [self.taskTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
  26.    
  27.     // Make the BNRAppDelegate the table view's dataSource
  28.     self.taskTable.dataSource = self;
  29.    
  30.     // Create and configure text field
  31.     self.taskField = [[UITextField alloc] initWithFrame:fieldFrame];
  32.     self.taskField.borderStyle = UITextBorderStyleRoundedRect;
  33.     self.taskField.placeholder = @"Type a task, tap insert";
  34.  
  35.    
  36.     // Create and initialize button with given type.
  37.     self.insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  38.     // Give the button a title and a state
  39.     [self.insertButton setTitle:@"Insert" forState:UIControlStateNormal];
  40.     // give the button a frame
  41.     self.insertButton.frame = buttonFrame;
  42.    
  43.     // Set the target and action for the Insert button.
  44.     [self.insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];
  45.    
  46.     // Add our three UI elements to the window
  47.     [self.window addSubview:self.taskTable];
  48.     [self.window addSubview:self.taskField];
  49.     [self.window addSubview:self.insertButton];
  50.    
  51.    
  52.     self.window.backgroundColor = [UIColor whiteColor];
  53.     [self.window makeKeyAndVisible];
  54.     return YES;
  55. }
  56.  
  57. - (void)applicationWillResignActive:(UIApplication *)application
  58. {
  59.     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  60.     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  61. }
  62.  
  63. - (void)applicationDidEnterBackground:(UIApplication *)application
  64. {
  65.     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  66.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  67. }
  68.  
  69. - (void)applicationWillEnterForeground:(UIApplication *)application
  70. {
  71.     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  72. }
  73.  
  74. - (void)applicationDidBecomeActive:(UIApplication *)application
  75. {
  76.     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  77. }
  78.  
  79. - (void)applicationWillTerminate:(UIApplication *)application
  80. {
  81.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  82. }
  83.  
  84.  
  85. #pragma mark - actions
  86. - (void) addTask:(id)sender{
  87.     // Get the text
  88.     NSString *text = [self.taskField text];
  89.    
  90.     // Quit here if taskField is empty
  91.     if([text length] == 0){
  92.         return;
  93.     }
  94.     // Add the text of text field to the working array
  95.     [self.tasks addObject:text];
  96.     // Refresh the table so that the new item shows up
  97.     [self.taskTable reloadData];
  98.    
  99.     // Clear out the textfield
  100.     [self.taskField setText:@""];
  101.    
  102.     // Dismiss the keyboard
  103.     [self.taskField resignFirstResponder];
  104.    
  105. }
  106.  
  107.  
  108. #pragma mark - Table view management
  109.  
  110. - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  111.     return [self.tasks count]; // returns the number of items in tasks array
  112. }
  113.  
  114. - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  115.     // Get a reusable cell
  116.     UITableViewCell *c = [self.taskTable dequeueReusableCellWithIdentifier:@"Cell"];
  117.    
  118.  
  119.     NSString *item = [self.tasks objectAtIndex:indexPath.row];
  120.     c.textLabel.text = item;
  121.    
  122.    
  123.     return c;
  124. }
  125. @end
Advertisement
Add Comment
Please, Sign In to add comment