2013-06-19 80 views
0

我有很多textViewsscrollView之上,並且每個textView都有一個自定義按鈕,以便用戶單擊該按鈕時應該展開,並且當它向後按時,它應該摺疊到之前的位置。如何通過點擊一個按鈕使textView擴展

我在想什麼做的是隱藏smallTextView並顯示expandedTextView當按下按鈕並按下按鈕時我想隱藏expandedtextViewñ顯示smallTextView。但我不知道該怎麼做。任何幫助將不勝感激。

這裏是我的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    self.title = @"Demo"; 
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    for (int i=0;i<[appDelegate.serverResponseArray count];i++) 
    { 
    self.expandTextView = [[UITextView alloc] init]; 
    [self.expandTextView setFrame:CGRectMake(8.0f, i*50.0f+10.0f, 270.0f, 40.0f)]; 
    [self.expandTextView setBackgroundColor:[UIColor grayColor]]; 
    [self.expandTextView setFont:[UIFont fontWithName:@"helvetica" size:12]]; 
    [self.expandTextView setText:@"Welcome!!!"]; 
    [self.expandTextView setTextColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1]]; 
    [self.expandTextView setUserInteractionEnabled:NO]; 
    [self.scrollView addSubview:self.expandTextView]; 
    self.expandTextView = nil; 

    self.expandButton = [[UIButton alloc]initWithFrame:CGRectMake(8.0f, i*50.0f+1.0f, 270.0f, 60.0f)]; 
    [self.expandButton setBackgroundColor:[UIColor clearColor]]; 
    [self.expandButton addTarget:self action:@selector(textButtonClicked:) forControlEvents:UIControlEventTouchDragInside]; 
    [self.scrollView addSubview:self.expandButton]; 

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(278.0f, i*50.0f+10.0f, 14.0f, 40.0f)]; 
    [imageView setBackgroundColor:[UIColor grayColor]]; 
    [imageView setImage:[UIImage imageNamed:@"arrow.png"]]; 
    [self.scrollView addSubview:imageView]; 
    imageView = nil; 

} 


float maxHeight = 0; 

    for(UIView *v in [self.scrollView subviews]) 
    { 
     if(v.frame.origin.x + v.frame.size.height > maxHeight) 
      maxHeight = v.frame.origin.x + v.frame.size.height; 
    } 

    self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, maxHeight+2570); 

} 
-(void)textButtonClicked:(id)sender 
{ 
    NSLog(@"Hi"); 
    [self.expandTextView setHidden:YES\]; 
    NSLog(@"hey"); 
} 

in this image every imageView has a custom button over it 和我怎麼知道是越來越按下哪個按鈕。

回答

1

你做錯了。你的方法應該是這樣的:

-(IBAction)textButtonClicked:(id)sender 
{ 
    NSLog(@"Hi"); 
    [self.expandTextView setHidden:YES]; 
    NSLog(@"hey"); 
} 

ViewContrller,線材你展開按鈕的-touchUpInside方法寫這個方法-textButtonClicked方法之後。使用此行:

[self.expandButton addTarget:self action:@selector(textButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

代替代碼中的一個。

+0

好的。會做到這一點。 – Zac24

+0

我糾正了我的錯誤,但仍然無法正常工作。 – Zac24

+0

我提供了代碼來正確調用您的按鈕單擊方法。現在,你必須添加一些邏輯。 – Bhavin