【问题标题】:Property 'delegate' object not found on type 'UIViewController *'在“UIViewController *”类型上找不到属性“委托”对象
【发布时间】:2014-04-01 18:55:05
【问题描述】:

我在运行项目时收到以下错误,如secView.delegate=self; 行中所示
我该如何解决上述问题?谢谢!

在类型“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];


              }

}

【问题讨论】:

    标签: ios objective-c uiviewcontroller


    【解决方案1】:

    当您声明secView 时,您将其定义为UIViewController *secView - 即一个普通的UIViewController。因此,就编译器而言,它没有任何自定义属性。

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

    BookmarkViewController *secView = ...;
    

    【讨论】:

    • 太棒了,它就像一个魅力。非常感谢!
    • 只需替换 UIViewController *menuVC = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];在 MenuViewController *menuVC = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 2014-04-27
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多