以编程方式添加按钮无法在导航根视图中工作原因?

时间:2010-07-11 00:36:58

标签: iphone xcode uibutton

CGRect frame = CGRectMake(10.0, 30.0, 32.0, 29.0);
UIImage *buttonImage = [UIImage imageNamed:@"visit_icon.png"];
UIButton *stopButton = [[UIButton alloc] initWithFrame:frame];
[stopButton setTitle:@"Button" forState:UIControlStateNormal];
[stopButton setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
[stopButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
stopButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
stopButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[stopButton addTarget:self action:@selector(stopAction:) forControlEvents:UIControlEventTouchUpInside];

我正在添加按钮到我的根视图控制器不起作用  可能我需要将此作为子视图添加到我的视图中 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上。

在上面代码末尾的UIViewController子类中执行此操作:

[self.view addSubview: stopButton];