今天弄一個實驗,在單元點擊 實作跳轉到網頁,但是總是崩潰到main
下面是四個頁面的代碼
ViewController.h代碼
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
{
NSArray *curProv;
}
@property(nonatomic,strong)NSDictionary *provCities;
@end
ViewController.m代碼
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(NSDictionary *)provCities
{
if (_provCities==nil) {
NSString *fileName=[[NSBundle mainBundle]pathForResource:@"prov_cities" ofType:@"plist"];
_provCities=[[NSDictionary alloc]initWithContentsOfFile:fileName];
}
return _provCities;
}
-(void)viewDidAppear:(BOOL)animated
{
switch(self.view.tag){
case 2:
curProv=self.provCities[@"遼寧省"];
self.title=@"遼寧省資訊";
break;
case 3:
curProv=self.provCities[@"黑龍江省"];
self.title=@"黑龍江省資訊";
break;
case 1:
curProv=self.provCities[@"吉林省"];
self.title=@"吉林省資訊";
break;
default:
break;
}
NSLog(@"%@",self.title);
NSLog(@"%@",curProv);
[self.tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [curProv count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
NSDictionary *cityDic=[curProv objectAtIndex:indexPath.row];
cell.textLabel.text=cityDic[@"name"];
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"city"])
{
DetailViewController *dvc=segue.destinationViewController;
NSIndexPath *indexPath=self.tableView.indexPathsForVisibleRows.firstObject;
NSDictionary *cityDic=[curProv objectAtIndex:indexPath.row];
dvc.cityDic=cityDic;
}
}
//-(void)viewDidLoad {
// [super viewDidLoad];
// // Do any additional setup after loading the view, typically from a nib.
//}
@end
DetailViewController.h代碼‘
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *WebVIew;
@property(nonatomic, strong)NSDictionary *cityDic;
@end
NS_ASSUME_NONNULL_END
DetailViewController.m代碼
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=self.cityDic[@"name"];
NSString *urlString=self.cityDic[@"url"];
NSURL *url=[NSURL URLWithString:urlString];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self.WebVIew loadRequest:request];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
uj5u.com熱心網友回復:
我建議你在DetailViewController的ViewDidLoad打個斷點,還有ViewController的prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender這個方法也打上斷點,除錯下轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/39029.html
標籤:iOS
上一篇:這微信開發者工具咋回事兒?網挺好的,這二維碼獲取不了。求幫我解決
下一篇:Qt 指定列印機列印PDF
