在iPhone上创建本地通知

时间:2013-11-16 01:12:13

标签: ios uilocalnotification

我正在尝试创建一个本地通知按钮,例如“您确定要删除是或否?我将如何执行此操作

4 个答案:

答案 0 :(得分:2)

试试这个:

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Are you sure you want to delete?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes", @"No", nil];
 alert.tag=1;
 [alert show];


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==1)
    {
       if(buttonIndex==0)
       {
             //Do when click "Yes" button
       }
       else if(buttonIndex==1)
       {
             //Do when click "No" button
       }
   }
}

可能会帮助你。

答案 1 :(得分:1)

根据您要完成的任务,我认为您不想使用UILocalNotification,看起来您想要的是UIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My title" message:@"Are you sure you want to delete?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes", @"No", nil];
[alert show];

希望有所帮助

答案 2 :(得分:1)

你可以使用UIAlertView来做你想做的事,就像Oscar Gomez所说的那样。

要向按钮添加操作,请首先实现UIAlertViewDelegate并使用方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

您可以检查buttonIndex并执行相应的操作(索引0表示按钮 “是”,按钮“否”为1。

答案 3 :(得分:0)

AppDelegate文件中的此代码块:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    // Override point for customization after application launch.
    return YES;
}

// This code block is invoked when application is in foreground (active-mode) 
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    **UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@“Alert” message:@“Are you sure you want to delete?” delegate:self cancelButtonTitle:nil otherButtonTitles:@“Yes”,@“No”, nil];

     notificationAlert.tag = 101;
    [notificationAlert show];**
   // NSLog(@"didReceiveLocalNotification");
}

**- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if(alertView.tag==1) {
        if(buttonIndex==0)
        {
            //Do when click "Yes" button
        }
        else if(buttonIndex==1)
        {
            //Do when click "No" button
        }
    }
}**

任何ViewController的.m文件中的代码块:

-(IBAction)startLocalNotification {  // Bind this method to UIButton action
    NSLog(@"startLocalNotification");

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
    notification.alertBody = @"This is local notification!";
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 10;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];    
}

当按下绑定“startLocalNotification”的按钮时,上面的代码在7秒的时间间隔后显示AlertView。 如果应用程序在后台,那么它将BadgeNumber显示为10并且默认通知声音。