2014-04-01 54 views
0

我在运行项目时出现以下错误,在行中指示secView.delegate=self;
我该如何解决上述问题?谢谢!在'UIViewController *'类型上找不到属性'委托'对象

属性 '代理' 的对象不是类型 '的UIViewController *'

myViewController.h

@interface myViewController : UIViewController <UITextFieldDelegate,UIAlertViewDelegate,secondViewControllerDelegate> 
- (IBAction)popBookmarkTable:(id)sender; 

myViewController.m

- (IBAction)popBookmarkTable:(id)sender { 
    UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]]; 
    UIViewController *secView = [st instantiateViewControllerWithIdentifier:@"bookmarkViewController"]; 
[self presentViewController:secView animated:YES completion:nil]; 

    secView.delegate=self; 
} 

- (void)passData:(NSString *)data 
{ 

      _addressBar.text=data; 

    } 

发现bookmarkViewController.h

@protocol secondViewControllerDelegate <NSObject> 

@required 

- (void)passData:(NSString *)data; 

@end 
@property (nonatomic, weak) id<secondViewControllerDelegate> delegate; 
@property (nonatomic,strong) NSString *a; 
@end 

bookmarkViewController.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; 
       if ([indexPath row]==0) { 
        _a=(NSString *)cell.textLabel.text; 
        [_delegate passData:_a]; 
        [self dismissViewControllerAnimated:YES completion:nil]; 


       } 

} 

回答

3

在声明secView,你把它定义为UIViewController *secView - 即一个纯UIViewController。因此,就编译器而言,它没有任何自定义属性。

您需要将其声明为特定的类类型,这将是在运行时:

BookmarkViewController *secView = ...; 
+0

真棒,它的工作就像一个魅力。非常感谢你! – floyddd

+0

只需替换 UIViewController * menuVC = [[MenuViewController alloc] initWithNibName:@“MenuViewController”bundle:nil]; 在 MenuViewController * menuVC = [[MenuViewController alloc] initWithNibName:@“MenuViewController”bundle:nil]; – dip

1

您需要进一步范围你secView对象。 UIViewController不声明委托属性,但您的协议。如果你确定视图控制器将实现该协议,试试这个:

UIViewController<secondViewControllerDelegate> *secView = [st instantiateViewControllerWithIdentifier:@"bookmarkViewController"]; 
相关问题