IOS学习-HelloWorld

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 --> Show Assistant 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;
}

点击运行,点击按钮,完成

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值