iOS 使用tableview和实现两个页面跳转时错误记录
1 使用tableview是一定要设置相应的delgate和datasource,如图实现一个通讯录时一般都需要用到tableview;
2 当我们某个程序需要实现某个协议时,此处以实现协议UISearchBarDelegate为例:
@interface BIDMasterViewController :UITableViewController<UISearchBarDelegate>
@property (weak, nonatomic) IBOutletUISearchBar *search;
//add your own code;
@end
self.search.delegate=self;//这一步一定不能省略(即表示委托给谁来实现协议中的方法,此处是委托给该控制器自身)
3 实现两个页面间的跳转两种方法介绍
首先给出需要跳转的两个视图,如下:
跳转方法之一是用代码来实现,主要代码如下:
UIStoryboard *board=[UIStoryboardstoryboardWithName:@"Storyboard"bundle:nil]; //获取跳转前的视图,@“ ”中的string是视图对应的identity如上图中靠右边的属性设置那一栏中有 Identity一栏,即可设置视图的唯一标识符
UIViewController *next=[boardinstantiateViewControllerWithIdentifier:@"detailViewController"];//获取跳转的目的视图
[self.navigationControllerpushViewController:nextanimated:YES];
@interface BIDDetailViewController :UITableViewController //由上面的图可知子视图中并没有使用到TableView
正确写法是:
@interface BIDDetailViewController :UIViewController
跳转的方法之二是在UI设置中直接将两个视图用segue连接即可。
而对于跳转中页面传值可以在下面的方法中实现:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//add your own code
id theSegue=segue.destinationViewController;
[theSegue setValue:传递的变量名 forKey:@"关键字"];
}