Make a link for each UITableViewCell with another view

烂漫一生 提交于 2019-12-13 04:52:46

问题


I'm new in iOS programming, and I want to do a simple thing. I saw several topics about my problem, but I don't understand why my code doesn't work...

  • I created a UIViewController subclass called details2ViewController, with a .xib
  • In my main view called ViewController.xib, I have a tableView
  • In ViewController.m, I added on the top : #import "details2ViewController.h"
  • In ViewController.m, I modified the didSelectRowAtIndexPath method like this :

    details *det = [[details alloc] init];
    [self.navigationController pushViewController:det animated:YES];
    

There is no warning, but no effect when I click on a cell... I precise that I'm working without mainStoryBoard.

note : Here's my previously post about this problem.

(Sorry if my english is awkward, I'm french... Thanks for your help !)


回答1:


First of all check that you have properly wired your TableView's delegate to your ViewController's File Owner or not. TableView's -didSelectRowAtIndexPath method is a delegate method.

Secondly, I am not getting why are using details as a Class name when you already have imported details2ViewController.h. So, It looks like you should use details2ViewController instead details and your code should look like this :

 details2ViewController *det = [[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil];
[self.navigationController pushViewController:det animated:YES];

Make sure that your ViewController is indeed embedded in a UINavigationController. If not then your self.navigationController will be nil.

Or you can go through An Introduction To UINavigationController.




回答2:


if you are using Storyboard you can use this code.

- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YourViewController *yourViewController = [storyboard instantiateViewControllerWithIdentifier:@"StoryBoardID"];
[self.navigationController pushViewController:yourViewController animated:YES];
}

Don't forget to set animated:BOOL to YES in last line.




回答3:


You have to first define a property of navigation controller in your AppDelegate.h:

@property (strong, nonatomic) UINavigationController *controller;

Then in the AppDelegate.m file do this in the didFinishLaunchingWithOptions: method :

details2ViewController *controller=[[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil];
    self.controller = [[UINavigationController alloc] initWithRootViewController:controller];
}

self.window.rootViewController=self.controller;
[self.window makeKeyAndVisible];
return YES;

Then in the details2ViewController.m in the didSelectRowAtIndexPath:

details2ViewController *det = [[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil];
[self.navigationController pushViewController:det animated:YES];

Do let me know if it work.. :) Thanks.



来源:https://stackoverflow.com/questions/18961798/make-a-link-for-each-uitableviewcell-with-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!