2017-01-04 10 views
1

このシナリオを実行してください。 UIViewControllerユーザーがボタンをクリックしたときに表示を動的に作成および削除する方法

  1. UIScrollViewがあり、それ内部UIViewUIButtonがあります。ユーザがUIButtonにタップすると、新しいUIViewをx、invialViewWidth + 30UIButton同じで添加されるべきです。

  2. ユーザーが2番目のUIButtonをクリックすると、新しい3番目のUIViewと3番目のUIButtonを作成する必要があります。同様に、n個のビューを作成する必要があります。

  3. ビュー内には、別のUIButtonの削除があります。クリックするとUIViewUIScrollViewから削除する必要があります。あなたは第五UIViewを削除したい場合は

たとえば、あなたは第五UIViewにある削除UIButtonをクリックしてください必要があります。

私はたくさんの質問をしましたが、私は正しい答えを得ていません。

+0

あなたは私が重複質問を見つけるいけない、私はscrollview私は、これは新しい問題だと思う – Matt

+0

@mattを好む必要があります:あなたはこのような何かを行うことができます。 –

+0

@danhを使用する必要が 'UITableView' –

答えて

0

ボタンを2つ追加して削除するカスタムビューを作成します。

-(UIView *)getNewViewWithTag:(NSInteger)tag{ 
     UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];//Set appropriate frame 
     UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     addButton.frame = CGRectMake(0,0,50,50); //Set appropriate frame 
     addButton.tag = tag; 
     [addButton addTarget:self action:@selector(addNewView:) forControlEvents:UIControlEventTouchUpInside]; 
     [containerView addSubview:addButton]; 

     UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     deleteButton.frame = CGRectMake(50,0,50,50); //Set appropriate frame 
     deleteButton.tag = tag; 
     [deleteButton addTarget:self action:@selector(deleteView:) forControlEvents:UIControlEventTouchUpInside]; 
     [containerView addSubview:deleteButton]; 
     containerView.tag = tag; 
     return containerView; 
    } 
    -(void)addNewView:(id)button{ 
     NSInteger tag = [button tag]; 
     UIView *view = [self getNewViewWithTag:tag+1]; 
     //set appropriate frame 
     [scrollView addSubview: view]; 
    } 
    -(void)deleteView:(id)button{ 
     NSInteger tag = [button tag]; 
     UIView *viewToDelete = [scrollView viewWithTag:tag]; 
     [viewToDelete removeFromSuperview]; 
    } 
+0

速い応答ありがとうございます、私は確認してお知らせします –

+0

@Hi rahul、ビューを検出する際に問題があります。問題は、5番目のビューで削除ボタンを押すと5番目のビューは4番目のビューを削除する必要があります。 –

+0

追加ボタンをクリックすると最初のビューが作成され、最初のビューの追加ボタンは非表示にする必要があります –

関連する問題