flyers
flyers

Reputation: 514

UIDatePicker Scheduler Error

I have followed this tutorial http://ios.biomsoft.com/2011/08/27/iphone-programming-tutorial-–-local-notifications/ I am able to run the app, however in order to run the app I have remove the following code in my appdelegate.m

enter image description here

You can see the error in the picture above.

Why do I receive the error and how do I fix the error?

Here is my code.

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *tableview;
IBOutlet UIDatePicker *datePicker;
IBOutlet UITextField *eventText;
}

@property (nonatomic, retain) IBOutlet UITableView *tableview;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) IBOutlet UITextField *eventText;

- (IBAction) scheduleAlarm:(id) sender;

@end

.m

@implementation ViewController

@synthesize datePicker,tableview, eventText;

- (IBAction) scheduleAlarm:(id) sender {

[eventText resignFirstResponder];

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

// Get the current date
NSDate *pickerDate = [self.datePicker date];

// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | 
NSMonthCalendarUnit |  NSDayCalendarUnit )

fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | 
NSMinuteCalendarUnit | NSSecondCalendarUnit )

fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];

// Notification details
localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = @"View";

localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;

// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue"  
forKey:@"someKey"];
localNotif.userInfo = infoDict;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

[self.tableview reloadData];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
datePicker = nil;
tableview = nil;
eventText = nil;
}

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// We only have one section
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of notifications
return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   
*)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
reuseIdentifier:CellIdentifier];
}

// Get list of local notifications
NSArray *notificationArray = [[UIApplication sharedApplication]    
scheduledLocalNotifications];
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];

// Display notification info
[cell.textLabel setText:notif.alertBody];
[cell.detailTextLabel setText:[notif.fireDate description]];

return cell;
}
@end

Upvotes: 0

Views: 80

Answers (3)

flyers
flyers

Reputation: 514

Ok so after doing some playing around. I was able to get the app to run without having to change the app delegate at all.

Not sure why in the tutorial he states to do so.

Thanks for you help.

Upvotes: 0

Hiren
Hiren

Reputation: 12780

please check your AppDelegate code first

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

   /******
       YOUR CODE HERE

   ******/

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];


    return YES;
}

Upvotes: 0

tilo
tilo

Reputation: 14169

You have to create an instance of the viewControllerfirst:

ViewController *viewController = [[ViewController alloc] init];
// set the frame, do other stuff..
[_window addSubview:viewController.view];

Upvotes: 1

Related Questions