2

我正在添加一个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");
}

我的情况是

MyMainViewControllersourceViewController为我创建的customSegue,这是UIStoryboardSegue

MyMainViewController有一个 UIView PlaceHolderView ,我在其中添加了MydestinationViewContoller这个 Scrollview 的 View

-(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;

让它工作

4

4 回答 4

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

于 2012-08-30T06:06:01.217 回答
1

你的动作需要接受一个(id) sender参数,即使你没有使用它:

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

addSubScrollView

[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
于 2012-08-30T04:40:53.210 回答
1

按钮目标应该喜欢

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

尝试这个。

更新:-

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

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

现在工作我检查了。

于 2012-08-30T04:42:07.113 回答
0

代替

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

写这个,

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

为了更好的练习,总是写这个,

-(void) btnSubImageClicked:(id)sender{}
于 2012-08-30T04:53:16.000 回答