IOS 下的 HelloWorld程序 续 事件处理

继续上次的Helloworld程序,下面我们将此应用程序添加Button事件!

和我一起开始做:

1.继续上次的教程,因为我们上次已经创建 hellobailu label控件所以,打开xib文件 我们直接将未创建的,button 创建出来!

    我们创建两个button控件 ,一个名字叫showlabel 另一个叫 return!分别的作用是 一个显示 label的内容,一个返回(改变)label 的内容。

将button 控件直接拖拽到虚拟界面上 任意位置添加。

2.添加成功后点击模拟界面上的,右侧就会出现相对的,button 属性,我们根据自己想需要修改相应属性。然后我们只将 title 修改下,改为 showlabel。。

就会发现button 上相应的按钮内容也会改变。

 

3.下面为我们需要用到的连个label事件做准备,打开 ViewController.h 首先创建两个标签 ilabel 和 rlabel 简单说下这两个标签的功能,也就是说

在原来显示的 hello bailu babel上面显示 单击按钮后改变的内容,当我们点击showlabel 时 label 显示“bailu welcome ”当单击按钮 return 时 label显示其他内容。

4.创建完成后,单击xib文件,右键单击框框的file Own文件,就会出现如图的界面,然后简历关联,将ilabel和rlabel的的标签全部关联到 label标签上。

5.实现ilabel 和 rlabel 的方法。此处应该和第4步互换下位置,应该先实现此方法。

6.设置事件,

-(IBaction)showLabel:(id)sender{

ilabel.text=@"bailu welcome!!!";

意思就是,当点击showlabel按钮的时候,ilabel的内容将显示bailu welcome!

其实不难理解!

/****************************************************/

博客有一个地方给大家介绍错误了,给button添加事件的时候;

-(IBAction)Return:(id)sender{}

这里的.m文件里的 return 和 xib 里面的按钮显示的内容没有关系!

当建立关联的时候不需要 return 这个事件名 不需要和 xib中的按钮内容相同 

/***************************************************/

下面的同样是这个道理!

7.切换到xib文件将 showlabel 和 return 分别关联 showlabel 按钮(button) 和 return 按钮(button )完成关联!这里的事件触发方式就是按下按钮后,触发事件。

在控件加入事件监听的方法到这里就结束了!

 

 

有不明白的地方欢迎到群里交流:苹果IOS软件开发联盟 57636364

 我的微博 http://weibo.com/cnbailu

邮箱:cnbailu@163.com 

 

代码稍后附上!

 

// // main.m // Hello World_Code // #import #import "AppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } // // AppDelegate.h // Hello World_Code // #import @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @end // // AppDelegate.m // Hello World_Code // #import "AppDelegate.h" @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(320/2.0 - 140/2.0, 80, 140, 40)]; // 1 label.text = @"Hello World"; label.backgroundColor = [UIColor cyanColor]; label.textAlignment = NSTextAlignmentCenter; [self.window addSubview:label]; // 2 [label release]; // 1 return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值