Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "BNRAppDelegate.h"
- @implementation BNRAppDelegate
- #pragma mark - applciation delegate callbacks.
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- // Create empty array
- self.tasks = [[NSMutableArray alloc] init];
- // Create and configure the UIWindow instance.
- // a CGRect is a struct with an origin (x,y) and a size (width, height)
- CGRect winFrame = [[UIScreen mainScreen] bounds];
- UIWindow *theWindow = [[UIWindow alloc] initWithFrame:winFrame];
- self.window = theWindow;
- CGRect tableFrame = CGRectMake(0, 80, winFrame.size.width, winFrame.size.height - 100);
- CGRect fieldFrame = CGRectMake(20, 40, 200, 31);
- CGRect buttonFrame = CGRectMake(228, 40, 72, 31);
- // Create and configure table view
- self.taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
- self.taskTable.separatorStyle = UITableViewCellSeparatorStyleNone;
- [self.taskTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
- // Make the BNRAppDelegate the table view's dataSource
- self.taskTable.dataSource = self;
- // Create and configure text field
- self.taskField = [[UITextField alloc] initWithFrame:fieldFrame];
- self.taskField.borderStyle = UITextBorderStyleRoundedRect;
- self.taskField.placeholder = @"Type a task, tap insert";
- // Create and initialize button with given type.
- self.insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- // Give the button a title and a state
- [self.insertButton setTitle:@"Insert" forState:UIControlStateNormal];
- // give the button a frame
- self.insertButton.frame = buttonFrame;
- // Set the target and action for the Insert button.
- [self.insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];
- // Add our three UI elements to the window
- [self.window addSubview:self.taskTable];
- [self.window addSubview:self.taskField];
- [self.window addSubview:self.insertButton];
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- // 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.
- // 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.
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- // 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.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- // 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.
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- // 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.
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- }
- #pragma mark - actions
- - (void) addTask:(id)sender{
- // Get the text
- NSString *text = [self.taskField text];
- // Quit here if taskField is empty
- if([text length] == 0){
- return;
- }
- // Add the text of text field to the working array
- [self.tasks addObject:text];
- // Refresh the table so that the new item shows up
- [self.taskTable reloadData];
- // Clear out the textfield
- [self.taskField setText:@""];
- // Dismiss the keyboard
- [self.taskField resignFirstResponder];
- }
- #pragma mark - Table view management
- - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [self.tasks count]; // returns the number of items in tasks array
- }
- - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- // Get a reusable cell
- UITableViewCell *c = [self.taskTable dequeueReusableCellWithIdentifier:@"Cell"];
- NSString *item = [self.tasks objectAtIndex:indexPath.row];
- c.textLabel.text = item;
- return c;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment