今晚看了一些网上的教程进行了一下练习,一个简单的按钮,从创建到监听行为。在as3里面只是需要几行代码就可以,在object-c里面创建一个按钮有时候也不知道在哪添加,甚至第一次连写代码的地方也不知道。动态写一个按钮和as3 有很相似的动作。添加按钮,监听事件。这两个过程还是相似的。
学习oc的时候,我对此产生一个遗憾,创建一个实例的时候,java c# as3都是从new 开始,在oc 创建一个实例会有多种的方式。这个方式在学习的时候有一点疑惑和让人想不明白的地方。在赋值参数的,语法多少有点不适应的感觉。
下面粘贴一下代码,记录一下今晚学的东西。
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect ];
btn2.frame=CGRectMake(10, 23, 200, 40);
[btn2 setTitle:@"Test" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(testButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
}
-(void) testButtonClick
{
UIAlertView *alert2=[[UIAlertView alloc] initWithTitle:@"Hello world"
message:@"ipad i love you"
delegate:self
cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alert2 show];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction) btnClicked:(id) sender
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Hello world"
message:@"ipad i love you"
delegate:self
cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alert show];
}
@end