Dilshan
Dilshan

Reputation: 3221

UIButtonType issue

I have created a UIButton by passing an integer value as this.

 UIButton* custom_newBackButton = [UIButton buttonWithType:101];
 [custom_newBackButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
 [custom_newBackButton setTitle:@"Back" forState:UIControlStateNormal];

 UIBarButtonItem* newBackButton = [[UIBarButtonItem alloc] initWithCustomView:custom_newBackButton];
 [[self navigationItem] setLeftBarButtonItem: newBackButton];

In some of the classes this works but some of the classes it fails with "Invalid conversion from int to UIButtonType". Is this a recommended way to handle this. I have simply use this 101 to get the back button look and feel.

Regards,

Dilshan

Upvotes: 1

Views: 2612

Answers (2)

BastiBen
BastiBen

Reputation: 19880

The following button types are officially documented in the Apple documentation material:

typedef enum {
   UIButtonTypeCustom = 0,
   UIButtonTypeRoundedRect,
   UIButtonTypeDetailDisclosure,
   UIButtonTypeInfoLight,
   UIButtonTypeInfoDark,
   UIButtonTypeContactAdd,
} UIButtonType;

Enjoy! Please avoid using direct values. Values of constants might change and break your app.

Upvotes: 5

tia
tia

Reputation: 9698

Explicitly casting it to UIButtonType should work fine, though the constant is undocumented and would be rejected when submitting to App Store if the reviewer would aware of that.

Upvotes: 1

Related Questions