【问题标题】:unable to add UIButton on view无法在视图上添加 UIButton
【发布时间】:2013-11-07 12:37:34
【问题描述】:

我有一个名为HomeViewController 的类名,在HomeViewController 内我添加PageViewController(即APPChildViewController)

这是我的代码:

HomeViewController.h:

@interface HomeViewController : UIViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate>{
    NSUInteger Pageindex;
    APPChildViewController *childViewController;
}

@property (strong, nonatomic) UIPageViewController *pageController;

HomeViewController.m:

- (void)viewDidLoad
{
    APPChildViewController *initialViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.pageController.delegate = self;
    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    [self OnLoad]; //Method on APPChildViewController class
}

APPChildViewController:

-(void)OnLoad{

        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(20, 20, 130,130);
     // [button setTitleEdgeInsets:UIEdgeInsetsMake(20, 30, 00, 15)];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];

}

在我的PageViewController(即APPChildViewController)中编写此代码后,我无法在APPChildViewController 类中看到按钮。

我在哪里做错了?请帮助并提前致谢。

【问题讨论】:

  • 您尝试为按钮添加标题或至少添加背景颜色?
  • 哦,是的,真的,伙计,我忘了在添加背景颜色后检查它是否有效。
  • 方法名称不要以大写字母开头,也不要使用 On 前缀。 iOS 不是 DotNet。

标签: ios iphone ipad class uibutton


【解决方案1】:

您尝试为按钮添加标题或至少添加背景颜色? :)

【讨论】:

    【解决方案2】:

    由于OnLoad是APPChildViewController的方法,所以你应该使用[initialViewController OnLoad]

    【讨论】:

      【解决方案3】:

      您调用 HomeViewController 的 OnLoad 方法而不是 APPChildViewController - 您需要将方法调用更改为 [initialViewController OnLoad]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-07
        • 2013-11-11
        • 2011-08-13
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多