我似乎无法使系统使用我拥有的自定义创建按钮。我在.m文件的接口部分中有以下声明:
@property (weak, nonatomic) IBOutlet UIButton *useLocationButton;
@property (weak, nonatomic) IBOutlet UIButton *useAddressButton;
然后我的init函数有以下内容
- (id)init
{
if (self = [super initWithNibName:@"TCMDirectionsViewController" bundle:nil]){
[[self navigationItem] setTitle:@"Get Directions"];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel:)];
click = NO;
[[self navigationItem] setLeftBarButtonItem:cancelItem];
UIImage *buttonImage = [[UIImage imageNamed:@"bluebutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:@"bluebuttonHighlight.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[[self useLocationButton] setBackgroundImage:buttonImage forState:UIControlStateNormal];
[[self useLocationButton] setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[[self useAddressButton] setBackgroundImage:buttonImage forState:UIControlStateNormal];
[[self useAddressButton] setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[_locationManager startUpdatingLocation];
}
return self;
}
当我运行应用程序时,按钮是我在界面构建器中创建的默认白色按钮。
答案 0 :(得分:2)
在加载视图之前调用init
方法,因此useLocationButton
尚不存在。
将您的视图设置放在UIViewController
的{{3}}方法中,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonImage = [[UIImage imageNamed:@"bluebutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:@"bluebuttonHighlight.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[[self useLocationButton] setBackgroundImage:buttonImage forState:UIControlStateNormal];
[[self useLocationButton] setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[[self useAddressButton] setBackgroundImage:buttonImage forState:UIControlStateNormal];
[[self useAddressButton] setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
}
另外,请确保您的IBOutlets
实际已连接。您可能希望设置断点以检查引用是否有效。