UIButtonType问题

Dil*_*han 1 iphone cocoa-touch uibutton iphone-sdk-3.0 ios4

我通过传递一个整数值来创建一个UIButton.

 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];
Run Code Online (Sandbox Code Playgroud)

在某些类中,这可以工作,但是它失败的一些类"从int到UIButtonType的转换无效".这是一种推荐的方法来处理这个问题.我只是使用这个101来获得后退按钮的外观和感觉.

问候,

Dilshan

Bas*_*Ben 5

Apple文档材料中正式记录了以下按钮类型:

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

请享用!请避免使用直接值.常量值可能会更改并破坏您的应用程序.

  • 两者都指出记录的枚举,并建议使用常量名称而不是文字值. (2认同)