2013-04-11 51 views
0

我按照这个教程 http://ios.biomsoft.com/2011/08/27/iphone-programming-tutorial- --local的通知/ 我能够运行的应用程序,但为了运行应用程序,我必须删除我的appdelegate.m下面的代码的UIDatePicker调度错误

enter image description here

您可以在上图中看到错误。

为什么我收到错误,如何解决错误?

这是我的代码。

.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 

回答

1

你必须创建viewController第一的实例:

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

愚蠢的问题这个代码放在哪里? – flyers 2013-04-11 16:12:52

0

请检查你的AppDelegate代码首先

- (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; 
} 
+0

好的我仍然收到一个错误,但是一个不同的类型。 – flyers 2013-04-11 16:16:18

+0

你能告诉我吗? – Hiren 2013-04-11 16:18:10

+0

对不起,意外返回。 self.viewController = [[ViewController alloc] initWithNibName:@“ViewController”bundle:nil];必须将[[viewcontroller alloc]更改为[[ViewController alloc])。然后我收到一个错误说明:属性'viewController'找不到'AppDelegate *'类型的对象我也收到同样的错误在self.window.rootViewController = self.viewController; – flyers 2013-04-11 16:24:25

0

好了之后做了一些玩耍。我能够让应用程序运行而不必更改应用程序委托。

不确定为什么他在教程中声明要这样做。

感谢您的帮助。

+0

你演奏的是什么? – samfisher 2013-04-11 17:25:41

+0

我没有将BOOL添加到应用程序委托。 – flyers 2013-04-11 19:28:01