0

textViews在一个顶部有很多scrollView,每个textView都有一个自定义按钮,因此当用户单击textView它时它应该展开,当它单击返回时它应该折叠到以前的位置。

我想做的是隐藏smallTextView并显示expandedTextView按下按钮的时间,按下按钮时我想隐藏expandedtextViewn 显示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");
}

在这张图片中,每个 imageView 上面都有一个自定义按钮 以及我如何知道按下了哪个按钮。

4

1 回答 1

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

而不是你的代码中的那个。

于 2013-06-19T10:00:39.070 回答