以编程方式添加按钮在故事板中崩溃

时间:2012-08-30 04:31:46

标签: iphone objective-c ios uibutton storyboard

我在UIScrollView

中的StoryBoard添加了一个按钮

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

-(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是我创建的sourceViewController的{​​{1}},customSegue

UIStoryboardSegue将UIView设为MyMainViewController我在其中添加PlaceHolderView的视图,这是此Scrollview

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;

使其正常运作

4 个答案:

答案 0 :(得分:4)

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

[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];

答案 1 :(得分:1)

您的行动需要接受(id) sender参数,即使您不使用它:

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

addSubScrollView

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

答案 2 :(得分:1)

按钮目标应该是

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

试试这个。

更新: -

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

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

现在正在检查工作。

答案 3 :(得分:0)

而不是

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

写这个,

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

为了更好的练习,总是写下来,

-(void) btnSubImageClicked:(id)sender{}
相关问题