如何在xcode上创建一个按钮?

时间:2013-01-05 12:54:36

标签: iphone xcode

我在设计Xcode上的按钮时遇到了麻烦。

我正在使用以下代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundRect];
button.frame=CGRectMake(80.0,210,160,40);
[self.view addSubview:button];

代码是Build Successfully。但是这个错误即将来临:

  

主题1:EXC_BAD_ACCESS(代码= 2,地址= 0xbf7ffffdc)

2 个答案:

答案 0 :(得分:2)

这是您根据所需背景和文字颜色创建自定义按钮的方法。

 btn_fbLogin = [[UIButton alloc]initWithFrame:CGRectMake(95,254,130,41)];
[btn_fbLogin setTitle:@"FLogin" forState:UIControlStateNormal];
[btn_fbLogin addTarget:self action:@selector(FacebookLogin) forControlEvents:UIControlEventTouchUpInside];
[btn_fbLogin setBackgroundColor:[UIColor whiteColor]];
btn_fbLogin.clipsToBounds = YES;
btn_fbLogin.layer.cornerRadius = 5.0f;
[btn_fbLogin setTitleColor:[UIColor colorWithRed:244/255.0  green:185/255.0 blue:25/255.0 alpha:1.0] forState:UIControlStateNormal];
[btn_fbLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];     
[self.view addSubview:btn_fbLogin];

不要忘记导入#import <QuartzCore/QuartzCore.h>

希望这会有所帮助.. :)

答案 1 :(得分:0)

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0,210,160,40);
[button setTitle:@"Submit" forState:UIControlStateNormal];
[self.view addSubview:button];

你得到的错误似乎不是UIButton的原因。你能指定更多吗?