【问题标题】:why button in view inactive with parent view's controller?为什么视图中的按钮与父视图控制器处于非活动状态?
【发布时间】:2017-12-05 08:45:04
【问题描述】:

我正在使用swipView制作页面滚动应用

根据它的SwipeViewDataSource协议,我做了一个方法

 - (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{

        ContentViewController *contentVC = [[ContentViewController alloc]init];
        view = contentVC.view;
        return view;
}

提供刷屏内容

ContentViewController 很简单:

它只有这两个自定义方法:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
    [btn setTitle:@"click me" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor redColor]];
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}
-(void) onClick:(id) sender {
    NSLog(@"click detected in contentView controller");
}

只在viewDidLoad中添加了一个按钮和点击监听方法

运行应用程序时,显示按钮,但单击时不要触发OnClick: 方法。

琢磨了一下,发现如果我把这个方法放了

-(void) onClick:(id)sender {
    NSLog(@"on click detected  main view controller");
}

到实现SwipeViewDataSource 的视图,即在初始化ContentViewController 的“父级”中,将打印该行调试信息。

对我来说,这很奇怪。我的意思是为什么会这样?

【问题讨论】:

  • 创建 ContentViewController 的强属性并尝试

标签: ios objective-c viewcontroller


【解决方案1】:

您的 contentviewcontroller 将在 viewForItemAtIndex 委托方法中返回其视图后释放,因为您仅保留其视图。但是您正在将按钮目标添加到已经发布的 self(contentviewcontroller) 中。

【讨论】:

  • 那为什么调用父视图控制器上的方法呢?
猜你喜欢
  • 2013-03-30
  • 2017-08-25
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多