tags:

views:

147

answers:

1

im new to iphone sdk programming, and have been stuck on this problem for days.

i want a button that toggles a uitextview. first click shows the textbox, second click hides it and so on.

it sounds very simple

A: 

Create an instance of UIButton, and set its target to a method that will toggle the UITextView instance's hidden property. For example, assume that you have a UITextView instance variable named disappearingTextView.

- (void)loadView
{
    [super loadView];

    // Add the UITextView.
    disappearingTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
    [[self view] addSubview:disappearingTextView];

    // Add the button, and add self as target, with toggleTextViewHidden as the action to trigger on TouchUpInside.
    UIButton *toggleTextViewHiddenButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [toggleTextViewHiddenButton setFrame:CGRectMake(10, 220, 300, 44)];
    [toggleTextViewHiddenButton addTarget:self action:@selector(toggleTextViewHidden) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:toggleTextViewHiddenButton];
}

Then, in the toggleTextViewHidden method, toggle the disappearingTextView's hidden property...

- (void)toggleTextViewHidden
{
    [disappearingTextView setHidden:( ! [disappearingTextView isHidden])];
}
ryanipete