2014-10-16 76 views

回答

0

你想看看UIAlertController。 UIActionSheet已弃用。它非常易于使用。

使用Obj-C,您可以使用conventience方法创建一个实例,将样式设置为actionSheet。然后在你的工作表上为每个“按钮”调用addAction。下面是一些代码,你可以从工作:

- (IBAction)actionButtonAction:(id)sender { 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Select an action" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Share Data" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
     // Your activity 
    }]]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Share Plot" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
     // Your activity 
    }]]; 



    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 
     // User cancelled 
    }]]; 

    // Present VC. 
    [self presentViewController:alertController animated:YES completion:^{ 

    }]; 
} 
0

,您就有了当应用程序启动(第一次启动时迅速打开项目视图控制器,你的故事板将有一个视图控制器看来,这是根控制器,并且在项目中生成的初始“viewController.swift”文件被连接到它。

从该根控制器(或任何视图控制器,您用作初始视图控制器)

您可以创建,配置并呈现在ViewDi中为ActionSheet样式配置的UIAlertController dAppear方法。

就这样

class ViewController: UIViewController { 

    override func viewDidLoad() { 

    super.viewDidLoad() 
    } 



override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 

    let userDefaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() 

    let firstTimeLoggingIn: Bool? = userDefaults.objectForKey("firstTimeLogin") as? Bool 

    if (firstTimeLoggingIn == nil) { 
     userDefaults.setBool(true, forKey: "firstTimeLogin") 
     actionSheetForFirstLogin() 
    } 

} 

func actionSheetForFirstLogin() { 
    let actionSheet: UIAlertController = UIAlertController(title: "the title", message: "the message", preferredStyle: .ActionSheet) 

    let callActionHandler = { (action:UIAlertAction!) -> Void in 
     let alertMessage = UIAlertController(title: action.title, message: "You chose an action", preferredStyle: .Alert) 
     alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     self.presentViewController(alertMessage, animated: true, completion: nil) 
    } 

    let action1: UIAlertAction = UIAlertAction(title: "action title 1", style: .Default, handler:callActionHandler) 

    let action2: UIAlertAction = UIAlertAction(title: "action title 2", style: .Default, handler:callActionHandler) 

    actionSheet.addAction(action1) 
    actionSheet.addAction(action2) 

    presentViewController(actionSheet, animated: true, completion:nil) 
} 

} 

这也为分离动作片的动作处理程序,所以你可以看到如何独立创建并添加他们。在这个例子中,我只创建了一个,并检查了操作标题以显示将根据选择显示的警报;但是,您始终可以创建单独的操作。

编辑

我已经添加到NSUserDefaults的处理在第一时间登录的情况

+0

谢谢domitall您的快速回复。我知道如何使用UIAlertController。我的问题是,我想只有当我的应用程序第一次以预定义的时间间隔启动时才会出现。 – spenumatsa 2014-10-18 01:15:24

+0

我不确定你的意思是预定义的时间间隔,如果一个人之前登录过的时间间隔可能是?无论哪种方式,我都添加了一个编辑来处理首次使用NSUserDefaults登录。如果你想在一段时间内运行它,你可以使用NSTimer和一个名为上面的“actionSheetForFirstLogin”方法的选择器 – domitall 2014-10-18 17:13:41

相关问题