花过无痕的专栏

1.新建项目ButtonFun

在xcode 中File –> New –> Project 选择Single View Application,,点击next,

在Product Name 栏输入ButtonFun,Device栏选择iphone,点击Next,选择项目存放位置,点击Create创建项目

2.连接xib和ViewController文件

xcode使用故事板,如果不需要直接删除即可,新建ViewController.xib : File –> New –>File 在弹出框中左侧选择User Interface ,右侧选择View,点击Next,输入名称ViewController点击create即可创建ViewController.xib文件

点击ViewController.xib文件,点击左侧第一个立方体File’s Owner,然后在右侧选项卡顶部第三个选项卡下Custom Class 的class 中输入ViewController确定即可。

在ViewController.m文件中viewDidLoad方法中添加如下代码即可加载xib文件:

– (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.UIView *rootView = [[[NSBundle mainBundle]loadNibNamed:@"ViewController" owner:self options:nil]lastObject];self.view = rootView;}

3.添加组件

打开ViewController.xib,从xcode的右下角第三个选项卡中将Button 和Label拖入xib中,

点击菜单栏View –> Assistant Editor –> ShowAssistant Editor 可以打开两个编辑栏,在左右侧分别显示ViewController.xib和ViewController.h文件,

按住control键 鼠标点击Button拖动会出现一根蓝色的线, 然后拖动到ViewController.h中,在弹出框中connection项选择Action,name栏输入buttonPressed点击connect;

再次拖动Button到buttonPressed方法中进行事件绑定;

相同方法将Label拖到ViewController.h中,弹出框中connection项选择Outlet,name栏输入statusLabel点击connect;

ViewController.h代码如下:

@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UILabel *statusLabel;- (IBAction)buttonPressed:(id)sender;@end

4.实现方法

现在打开ViewController.m方法,会有一个空的方法体:

– (IBAction)buttonPressed:(id)sender {}添加代码:

– (IBAction)buttonPressed:(id)sender {NSString *title = [sender titleForState:UIControlStateNormal];_statusLabel.text = plainText;}点击运行,点击按钮,完成

可就是这样,还是有人,期望过多的温暖。

花过无痕的专栏

相关文章:

你感兴趣的文章:

标签云: