开发者

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

开发者 https://www.devze.com 2023-03-20 20:52 出处:网络
I create & present modally an UITableViewController(say Table2) class from another UITableViewController class(say Table1) like this..

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号