2011年9月26日

宣告UIbutton的兩種方法

用Class Method:
UIButton *infoDoneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//這是Class Method,是自動alloc自動release
[infoDoneButton setTitle:@"Done" forState:UIControlStateNormal];
[infoDoneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[infoDoneButton setBackgroundColor:[UIColor whiteColor]];
[infoDoneButton addTarget:self
action:@selector(infoDismissAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoDoneButton];

也可以用Instance Method寫:

infoDoneButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 180, 200, 30)];
[infoDoneButton setFrame:CGRectMake(50, 180, 200, 30)];
[infoDoneButton setTitle:@"Done"
forState:UIControlStateNormal];
[infoDoneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[infoDoneButton setBackgroundColor:[UIColor whiteColor]];
[infoDoneButton addTarget:self
action:@selector(infoDismissAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoDoneButton];

0 意見:

張貼意見