xcode简单教程,实现一个button点击事件
2017-05-15 17:46
477 查看
1.新建工程
File->New->Project->SingleViewApplication->Next->ProductName->Next
2.ViewController添加按钮
viewDidLoad 方法添加:[self setupUI];
实现setupUI:
#pragma mark - Setup - (void)setupUI { // Hello button. UIButton *helloButton = [UIButton buttonWithType:UIButtonTypeSystem]; [helloButton setTitle:@"Hello" forState:UIControlStateNormal]; [helloButton addTarget:self action:@selector(onHelloButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:helloButton]; //// Layout with frame. //[helloButton setFrame:CGRectMake(0, 0, 60, 40)]; //helloButton.center = self.view.center; //// Layout with constraint. helloButton.translatesAutoresizingMaskIntoConstraints = NO; // If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to NO. [self.view addConstraints:@[ [NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.0], [NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.0], [NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0], [NSLayoutConstraint constraintWithItem:helloButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0] ]]; }
实现点击事件:
#pragma mark - Action - (void)onHelloButtonClicked:(id)sender { NSLog(@"Hello, world!"); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello" message:@"Hello, world!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancle Action"); }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"OK Action"); }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }
相关文章推荐
- OpenGJ的一个简单事例,实现点击变颜色
- 关于相同页面用一个页面实现,点击datalist中Button按钮出现“回发或回调参数无效......”
- 点击事件中实现弹出一个选择框(如选择网络设置、选择电话短信联系方式)
- 【Cocos2d-x v3.2+Cocos Studio1.6】实现一个简单的uibutton点击功能
- 纯js实现点击一个事件后,触发另外一个事件的方法
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- 在android中用代码触发一个按钮的点击事件的代码是: buttonName.performClick();
- 简单的实现listView中item多个控件以及点击事件
- Cocos2d-x V3.2+Cocos Studio1.6 实现一个简单的uibutton点击功能
- 自定义CursorAdapter,实现ListView中动态加载button,和点击事件
- xcode4.2中storyboard实现一个简单的跳转 iOS 5的StoryBoard
- Android ImageButton 如何实现一个点击效果
- alertdilog加载一个gridview页面并实现gridView中每个imagebutton的点击事件
- 点击button按钮后,启动一个实现鼠标拖动另一个button的线程 (JFrame/C# WinForm)
- PySide教程:一个简单的点击按钮示例
- PySide教程:一个简单的点击“.NET研究”按钮示例
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android-学习笔记之 Button实现点击事件的几种方式
- 在页面中,我们经常看到,一个button按钮,如果属标点击,就会触发一个窗口的显示,如果二次点击并可以隐藏,那么如何通过JAVA配合html来实现这一功能呢?