【发布时间】:2014-01-06 13:34:31
【问题描述】:
在 Xcode 5.0.2 中,我为 iPhone 创建了一个空白的 Detail-Master 应用程序。
然后在 Main.storyboard 中,我将 UILabel 替换为 UIWebView,并在 DetailViewController.h 中将其声明为 webView。
我还将id *defaultItem 替换为NSDictionary *dict:
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) NSDictionary *dict;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
在MasterViewController.m 中,我硬编码了一个“字典中的字典”_menu:
#import "MasterViewController.h"
#import "DetailViewController.h"
static NSString *kLabel = @"label";
static NSString *kAuthUrl = @"auth_url";
@interface MasterViewController () {
NSDictionary *_menu;
NSArray *_keys;
}
@end
@implementation MasterViewController
- (void)awakeFromNib
{
[super awakeFromNib];
_menu = @{
@"FB": @{
kLabel: @"Facebook",
kAuthUrl: @"https://graph.facebook.com/oauth/authorize?",
},
@"GG": @{
kLabel: @"Google+",
kAuthUrl: @"https://accounts.google.com/o/oauth2/auth?",
},
@"MR": @{
kLabel: @"Mail.ru",
kAuthUrl: @"https://connect.mail.ru/oauth/authorize?",
},
@"OK": @{
kLabel: @"Odnoklassniki",
kAuthUrl: @"http://www.odnoklassniki.ru/oauth/authorize?",
},
@"VK": @{
kLabel: @"VKontakte",
kAuthUrl: @"http://oauth.vk.com/authorize?",
},
};
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _keys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *key = _keys[indexPath.row];
NSString *label = _menu[key][kLabel];
cell.textLabel.text = label;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *key = _keys[indexPath.row];
NSDictionary *dict = _menu[key];
[[segue destinationViewController] setDetailItem:dict]; // XXX the error line
}
}
@end
最后在DetailViewController.m我尝试获取传递的NSDictionarydict并加载webView中的URL:
#import "DetailViewController.h"
static NSString *kLabel = @"label";
static NSString *kAuthUrl = @"auth_url";
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(NSDictionary*)newDict
{
if (_dict != newDict) {
_dict = newDict;
[self configureView];
}
}
- (void)configureView
{
if (_dict) {
NSURL *url = [NSURL URLWithString:_dict[kAuthUrl]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
}
}
@end
不幸的是,我在 Xcode 中遇到了编译时错误:
MasterViewController.m:没有已知的选择器“setDetailItem:”的实例方法
对于这一行:
[[segue destinationViewController] setDetailItem:dict];
为什么会这样?
UIStoryboardSegue 类的destinationViewController 属性声明为id 类型,它不应该接受任何选择器吗?
【问题讨论】:
-
它将接受任何选择器,但选择器仍需要在某处声明。
标签: ios iphone objective-c selector