【问题标题】:Property nil when using delegate to pass data back to VC使用委托将数据传回 VC 时的属性为零
【发布时间】:2014-01-28 05:38:06
【问题描述】:

我在 xib 文件中有一个视图,我将其加载到我的 viewController - 它是一个临时视图,因此不会一直显示在屏幕上。但是,临时视图和主视图使用相同的视图控制器类,因为它们都在做同样的工作。

现在,在 xib 视图中,我加载了第二个 viewControoler - 我们称之为 viewControllerB - 这是一个包含 197 个单元格的 UITableView。一旦选择了一个单元格,视图就会关闭,并且单元格的值会返回给 viewControllerA。

viewControllerB 在故事板上。

这是我使用的代码:

viewControllerB(我需要返回数据的 VC)

委托协议:

@protocol CountrySelectedDelegate <NSObject>

-(void)selectedCountry: (id)countryReturned;

@end

@property (strong, nonatomic) id <CountrySelectedDelegate> myDelegate;

然后在.m文件中:

设置委托:

ViewControllerA *viewA = [[ViewControllerA alloc]init];
    self.myDelegate = viewA;

然后我在同一个文件中调用委托;

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [[self myDelegate] selectedCountry:indexPath.row];


    [self dismissViewControllerAnimated:YES completion:nil];

}

现在在 viewControllerA 中:

pragma mark - CountrySelected 代表

-(void)selectedCountry:(id)countryReturned
{
    self.testInt = countryReturned;


}

现在这个方法确实被委托调用了。查看控制台: countryReturned 是正确的值。一旦这个方法完成,self.testInt 也是正确的值。

然后,在我的 viewWillAppear 方法中,我只需再次检查该值,看看它是否仍然与 NSLog 打印相同。

viewWillAppear 方法中的值始终为零(或我检查它的其他任何地方) - 我尝试过使用 NSMutableString 和 NSString - 始终为零。

为什么我的属性/实例变量设置为零?

更新

这就是我显示 viewControllerB 的方式

- (IBAction)countrySelect:(UIButton *)sender
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UITableViewController *vc = [sb instantiateViewControllerWithIdentifier:@"CountrySelection"];
    vc.modalTransitionStyle = UINavigationControllerOperationPop;
    [self presentViewController:vc animated:YES completion:NULL];
}

【问题讨论】:

  • 您如何将viewcontrollerB 显示为子视图或呈现。?
  • 对不起,我忘了显示。现在更新问题
  • 首先让你的 viewControllerB 成为 viewControllerA 中的一个强属性,这样它就不会在 countrySelect 方法完成后被释放。

标签: ios iphone objective-c delegates


【解决方案1】:

ViewB 上执行以下操作,我猜你已经完成了

@protocol CountrySelectedDelegate <NSObject>

-(void)selectedCountry: (NSInteger)countryReturned;

@end

@property (week, nonatomic) id <CountrySelectedDelegate> myDelegate;

didSelectRowAtIndexPath

if ([[self myDelegate] respondsToSelector:@selector(selectedCountry:)]) 
{
    [[self myDelegate] selectedCountry:indexPath.row];
}

开启ViewA

  1. 遵守协议

    @interface ViewA : UIViewController < CountrySelectedDelegate >
    
  2. 创建呼叫ViewB

    ViewB *viewBObjc=[[ViewB alloc] init];
    [viewBObjc setMyDelegate:self];
    [[self navigationController] presentViewController:ViewB animated:YES completion:Nil];
    
  3. ViewA上实现委托

    -(void)selectedCountry: (NSInteger)countryReturned
    {
        NSLog(@"Country Id is :%d", countryReturned);
    }
    

【讨论】:

  • 在 viewControllerB 中使用 self.myDelegate = self 会发出警告 - 分配强不兼容类型 viewcControllerB *const_strong'
  • 将委托设置为您想要取回价值的视图控制器。
  • 谢谢。但委托现在为零。
  • 好的,我先说清楚。您在 ViewA 上,并且您从要拉回数据的位置调用 ViewB。我说的对吗?
  • 正确 - viewA 调用 viewB。 viewB 中的 didSelectRowAtIndexPath 关闭视图 - 并调用委托。
【解决方案2】:

试试这个,

设置委托人

//in ViewControllerB
self.myDelegate = [self parentViewController];

- (IBAction)countrySelect:(UIButton *)sender
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    ViewControllerB *vc = (ViewControllerB *)[sb instantiateViewControllerWithIdentifier:@"CountrySelection"];
    vc.modalTransitionStyle = UINavigationControllerOperationPop;
    vc. myDelegate = self;
    [self presentViewController:vc animated:YES completion:NULL];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    相关资源
    最近更新 更多