【问题标题】:Programmatically added button getting crash in storyboard以编程方式添加的按钮在情节提要中崩溃
【发布时间】:2012-08-30 04:31:46
【问题描述】:

我在StoryBoard 中的UIScrollView 中添加一个按钮

下面是我正在使用的代码。

-(void)addScrollView
{
    for(int i=0;i<[array count];i++)
    {
        UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, SUBSCROLLVIEW_WIDTH, SUBSCROLLVIEW_HEIGHT)];
        UITextView *txtVwDetail = [[UITextView alloc] initWithFrame:CGRectMake(342, 0, TEXTVIEW_WIDTH, TEXTVIEW_HEIGHT)];
        txtVwDetail.text = SAMPLE_STRING;
        [self addSubScrollView:subScrollView];
        [self.view addSubview:subScrollView];
        [self.view addSubview:txtVwDetail];
     }
}

-(void)addSubScrollView:(UIScrollView *)aSubScrollView
{
    for(int i=0;i<[array count];i++)
    {
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame = CGRectMake(intBtnX, (aSubScrollView.frame.size.height - 80)/2, 50, 80);
        [aButton setImage:[UIImage imageNamed:[self.items objectAtIndex:i]] forState:UIControlStateNormal];
        **[aButton addTarget:self action:@selector(btnSubImageClicked) forControlEvents:UIControlEventTouchUpInside];**    
        aSubScrollView.contentSize = CGSizeMake(intScrollViewWidth, aSubScrollView.frame.size.height);
        [aSubScrollView addSubview:aButton];
    }
}

addSubScrollView 方法中,我添加了 Button 及其单击事件,该事件正在崩溃。

-(void)btnSubImageClicked
{
    NSLog(@"btnSubImageClicked");
}

我的场景是

MyMainViewController 是我创建的customSeguesourceViewController,这是UIStoryboardSegue

MyMainViewController 的 UIView 为 PlaceHolderView,我在其中添加了 MydestinationViewContoller 的视图,即此滚动视图

-(void)perform
{
    BrowseScreenVC *srcObj = (BrowseScreenVC *)[self sourceViewController];
    UIViewController *dstObj = (UIViewController *)[self destinationViewController];

    for(UIView *vw in srcObj.viewPlaceHolder.subviews)
    {
        [vw removeFromSuperview];
    }

    NSLog(@"dest : %@",dstObj.view);
    NSLog(@"destobj  :%@",dstObj);
    srcObj.currentViewController = dstObj;
    [srcObj.viewPlaceHolder addSubview:[dstObj.view retain]];

}

更新

有了答案,我还得换行 srcObj.currentViewController = dstObj;

srcObj.addChildViewController = dstObj;

让它工作

【问题讨论】:

  • 崩溃时控制台中的错误消息是什么?
  • 你能在模拟器中看到按钮吗?
  • *** -[MyViewController performSelector:withObject:withObject:]: 消息发送到已释放实例 0x6a7f2e0
  • 你的“自我”是什么?我希望它是 viewController。!!

标签: iphone objective-c ios uibutton storyboard


【解决方案1】:

你必须按如下方式添加目标:

[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];

@selector(),在这些大括号中,您必须提供您想要执行的方法。 “:”表示该方法有一些参数。在这种情况下,参数将是调用该方法的按钮本身。

你必须实现你的方法,然后它的签名看起来像这样

- (void)btnSubImageClicked:(id)sender{
// sender would be the button that is calling the method. you can use this object according to your requirements if you want. 

   // your code 
}

如果你也想从其他地方调用这个方法,你可以通过传递 sender 参数 nil 来调用它。例如[self btnSubImageClicked:nil];

【讨论】:

    【解决方案2】:

    您的操作需要接受 (id) sender 参数,即使您没有使用它:

    -(void)btnSubImageClicked:(id) sender
    {
        NSLog(@"btnSubImageClicked");
    }
    

    addSubScrollView

    [aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
    

    【讨论】:

    • 我有原始方法与发件人,但它正在崩溃,所以为了测试目的,我制作了没有发件人的方法。在这两种情况下它都崩溃了
    【解决方案3】:

    按钮目标应该喜欢

    -(void) btnSubImageClicked:(id)sender{}
    

    试试这个。

    更新:-

    请更正您的代码,更改此行

      [aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];  
    

    现在工作我检查了。

    【讨论】:

      【解决方案4】:

      代替

      UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

      写这个,

      UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

      为了更好的实践,总是写这个,

      -(void) btnSubImageClicked:(id)sender{}
      

      【讨论】:

        猜你喜欢
        • 2012-10-13
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 2017-02-10
        相关资源
        最近更新 更多