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");
}

私は次のようなシナリオを持っています

MyMainViewControllerクラスであるsourceViewController私の作成のためのものですcustomSegueUIStoryboardSegue

MyMainViewControllerこのScrollviewである'sViewをPlaceHolderView 追加しているUIViewを持っている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;

それを機能させるために

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 に答える