以编程方式将UIButton添加到具有故事板的自定义ViewController

时间:2014-06-26 15:30:35

标签: ios iphone objective-c xcode uibutton

我已按照AppCoda's Create UIPageViewController Using Storyboard tutorial成功实施了漫游屏幕。在示例的最后,在代码中创建了UIPageControl,现在我正在尝试以编程方式添加UIButton。

如果通过故事板添加按钮,则会显示该按钮,但是当(单独)添加到我的viewDidLoad时,这些按钮都不适用于我:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
// [self.view addSubview:button];
// [self.view bringSubviewToFront:button];
// [self.view insertSubview:button atIndex:0];
// [self.view insertSubview:button atIndex:[self.view subviews].count + 1];
// [self.view insertSubview:button aboveSubview:self.view];
// [self initButton:button];

我必须要有一些比较简单的东西。我应该坚持使用Android吗?

1 个答案:

答案 0 :(得分:1)

只需取消注释[self.view addSubview:button];行即可。 如果它无法正常工作,我相信你会在self.view上添加一些其他视图,这些视图会悬停在你的按钮上。