user2414460
user2414460

Reputation: 211

Can'T click textView when delegate is set

I have an App with 2 Views on top of each other and on the overlaying view there is a UITextView and a UIButton next to it.
When I click the Button once the TextView should become smaller
(from CGRectMake(0, 50, self.view.frame.bounds.size.width, 100)
to
CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)) and on the second click it should become bigger again.
Here's the code how I manage this:

-(IBAction)buttonTouched:(id)sender {
    if(counter == 1) {
    [self.textView removeFromSuperview];
    self.textView = nil;
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)];
    [self.overlayView addSubview:self.textView];
    counter = 2;
    } else {
        [self.textView removeFromSuperview];
        self.textView = nil;
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 100)];
        [self.overlayView addSubview:self.textView];
        counter = 1;
    }
}

My problem is that when I do it like this, I can't handle the delegate methods for the UITextView like textViewDidBeginEditing:(UITextView *)textView without setting the delegate. But when I set the delegate like self.textView.delegate = self after recreating the TextView, nothing happens when I click on it. No keyboard appears and the events aren't called either. Any suggestions?

Upvotes: 0

Views: 189

Answers (3)

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this,

Initialise and addSubview self.textView in viewDidLoad and set self.textView.delegate = self;

-(IBAction)buttonTouched:(id)sender {
    if(counter == 1) {

    self.textView.frame = CGRectMake(0, 50, self.view.frame.bounds.size.width, 50);
    counter = 2;
    } else {

        self.textView.frame = CGRectMake(0, 50, self.view.frame.bounds.size.width, 100);
        counter = 1;
    }
    [self.overlayView bringSubviewToFront:self.textView];
}

Just change the frame of self.textView in buttonTouched:

Upvotes: 0

sazz
sazz

Reputation: 3291

Try to alloc your textView in the ViewDidLoad,

self.textView = [[UITextView alloc]init];

and on the button click use

[self.textView setFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)];

and 

[self.textView setFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 100)];

Upvotes: 1

Bipin Patel
Bipin Patel

Reputation: 228

-(IBAction)buttonTouched:(id)sender {
    if(counter == 1) {
    [self.textView removeFromSuperview];
    self.textView = nil;
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)];
    [self.overlayView addSubview:self.textView];
    counter = 2;
    } else {
        [self.textView removeFromSuperview];
        self.textView = nil;
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 100)];
        [self.overlayView addSubview:self.textView];
        counter = 1;
    }
}
  • In above code only change "self.view.bounds.size.width" insted of "self.view.frame.bounds.size.width"
  • Assign delegate "self.textView.delegate=self" before that in .h file @interface ViewController : UIViewController

Upvotes: 0

Related Questions