xcode简单教程,实现一个button点击事件
来源:互联网 发布:js获取另一个页面的id 编辑:程序博客网 时间:2024/06/05 03:59
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];}
阅读全文
0 0
- xcode简单教程,实现一个button点击事件
- Button 简单按钮点击事件
- Button 点击事件的简单写法
- 实现expandablelistview中button点击事件
- Button点击事件的四种实现
- jquery实现button按钮的点击事件
- Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)
- Android实战简易教程<十五>(实现ListView中Button点击事件监听)
- RecyclerView实现简单点击事件
- Button的点击事件
- Button的点击事件
- Button点击事件写法
- Button的点击事件
- Button 点击事件(参数)
- android点击button事件
- Button点击事件
- Button的点击事件
- Button自动点击事件
- 【Linux】编译器参数配置
- Android studio NDK问题
- Python格式化输出
- 为何台湾在移动互联网时代远远落后于大陆?
- WMQ队列信息发送接收监听配置
- xcode简单教程,实现一个button点击事件
- 面向对象OOA、OOD、OOP
- SSL工作原理
- android7.0 开机后立刻恢复出厂设置,偶尔会失败
- 网页布局四之jQuery-easyUI的datagrid的基本应用
- iOS fmdb及工具MesaSQLite的使用
- Java的缓存与缓冲区
- sudo chown -R $(jianan) /usr/local
- 数据结构练习题5