简体   繁体   中英

How to put an UITableViewController in UIView to make touchesBegan to work in iPhone?

I create & present modally an UITableViewController(say Table2) class from another UITableViewController class(say Table1) like this..

-(void)createTable2 {
     Table2Controller *table2Controller = [ [Table2Controller alloc] initWithStyle:UITableViewStyleGrouped];
     UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:table2Controller];
     [self.navigationController presentModalViewController:nav animated:YES];
     [nav release];
     [table2Controller release];
}

So the Table2 will be shown. I want to use touchesBegan method to resign keyboard in Table2 because i have some UITextField as cells. I included UITextFieldDelegate protocol in.h file of Table2.

But i knew that these touchesBegan method will work only with UIView & not with UIViewController(Am i right?). But i dont know where & how(I tried in the createTable2 function itself. It does not work) to add an UIView and then add Table2 in that UIView to do things happen... Any advice....

Your table view controller has a table view property. You can subclass the table view and then override methods such as -touchesBegan:withEvent: . Instantiate your custom table view and set this instance to the view property.

UIViewController controls the UIView and other UI elements presented on the screen. All those elements can respond to touches thanks to UIResponder class that they are subclassing.

Use Table2Controller to override touchesBegan method to control the touch events that happen on any UI element within this viewController.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [inputTV resignFirstResponder];
    [super touchesBegan:touches withEvent:event];
}

Note always call super method declaration so that your touch can travel up the respond chain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM