我的iPhone应用程序仅当应用程序第一次启动时才需要显示操作表。我如何在Swift中完成这项工作?感谢您的帮助。iPhone应用程序第一次启动时显示操作表iOS 8
0
A
回答
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的处理在第一时间登录的情况
相关问题
- 1. 仅在第一次启动应用程序时显示UIAlertView
- 2. 如何在第一次应用程序启动时显示pushviewcontroller?
- 3. 在第一次启动iPhone应用程序时显示信息imageview
- 4. iphone应用程序在第一次启动时崩溃
- 5. 第一次启动iOS应用程序时检测吗?
- 6. 如何在第一次启动时执行Android应用程序时的操作
- 7. 当应用第一次启动时显示幻灯片教程
- 8. 仅在应用程序第一次启动时启动活动
- 9. 当第一次启动应用程序时,presentViewController不起作用
- 10. 显示设置并在第一次启动应用程序时记住
- 11. 应用程序第一次在应用程序启动后显示白屏的应用程序
- 12. 启动画面不显示每次启动的应用程序Iphone
- 13. 当应用程序第一次启动时调用didReceiveRemoteNotification
- 14. iOS通用应用程序 - 启动时显示空白屏幕
- 15. iPhone应用程序在第一次启动时需要登录吗?
- 16. 启动时和第一次启动应用程序时调度服务
- 17. 启动iPhone/iPad应用程序时提醒一次
- 18. NavigationBarItem在第一次应用程序启动时放错位置
- 19. 如何通过第一次应用程序启动显示对话框
- 20. 如何在首次启动应用程序时显示UIViewController?
- 21. 推送通知每次在应用程序启动时显示
- 22. 如何仅显示一次活动一次。应该只显示第一次打开的应用程序。?
- 23. 批次:启动应用程序不会显示一个GUI
- 24. iPhone位置数据在第一次启动时不起作用
- 25. setOnItemSelectedListener似乎只在第一次启动应用程序时才起作用
- 26. 3D触摸快速操作应用程序第一次启动时不起作用
- 27. 启动屏幕从第三方应用程序启动时重新显示
- 28. iOS - 每次应用打开时显示启动画面
- 29. iOS 8在应用程序启动时检测设备方向
- 30. iPhone应用程序在iOS 4.1上启动时崩溃
谢谢domitall您的快速回复。我知道如何使用UIAlertController。我的问题是,我想只有当我的应用程序第一次以预定义的时间间隔启动时才会出现。 – spenumatsa 2014-10-18 01:15:24
我不确定你的意思是预定义的时间间隔,如果一个人之前登录过的时间间隔可能是?无论哪种方式,我都添加了一个编辑来处理首次使用NSUserDefaults登录。如果你想在一段时间内运行它,你可以使用NSTimer和一个名为上面的“actionSheetForFirstLogin”方法的选择器 – domitall 2014-10-18 17:13:41