Ios 使用委托将数据传递回VC时的属性nil
我在一个xib文件中有一个视图,我将它加载到我的viewController中——它是一个临时视图,因此不总是在屏幕上。但是,临时视图和主视图使用相同的视图控制器类,因为它们都在执行相同的任务 现在,在xib视图中,我加载了第二个ViewController——我们称之为viewControllerB——这是一个包含197个单元格的UITableView。选择单元格后,视图将被取消,单元格的值将返回给viewControllerA viewControllerB位于故事板上 以下是我使用的代码: viewControllerB(我需要从中返回数据的VC) 代表协议:Ios 使用委托将数据传递回VC时的属性nil,ios,iphone,objective-c,delegates,Ios,Iphone,Objective C,Delegates,我在一个xib文件中有一个视图,我将它加载到我的viewController中——它是一个临时视图,因此不总是在屏幕上。但是,临时视图和主视图使用相同的视图控制器类,因为它们都在执行相同的任务 现在,在xib视图中,我加载了第二个ViewController——我们称之为viewControllerB——这是一个包含197个单元格的UITableView。选择单元格后,视图将被取消,单元格的值将返回给viewControllerA viewControllerB位于故事板上 以下是我使用的代码:
@protocol CountrySelectedDelegate <NSObject>
-(void)selectedCountry: (id)countryReturned;
@end
@property (strong, nonatomic) id <CountrySelectedDelegate> myDelegate;
然后我调用同一文件中的委托
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[self myDelegate] selectedCountry:indexPath.row];
[self dismissViewControllerAnimated:YES completion:nil];
}
现在在viewControllerA中:
pragma标记-选定代表
-(void)selectedCountry:(id)countryReturned
{
self.testInt = countryReturned;
}
现在这个方法确实被委托调用了。在控制台中查看:countryReturned是正确的值。此方法完成后,self.testInt
也是正确的值
然后,在我的viewWillExample方法中,我只需再次检查该值,看看它是否仍然与NSLog打印相同
VIEWWILLEXPENCE方法中的值始终为nil(或我检查它的任何其他位置)-我已尝试使用NSMutableString和NSString-始终为nil
为什么我的属性/实例变量设置为零
更新:
这就是我显示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];
}
在
ViewB
上执行以下操作我想您已经完成了
@protocol CountrySelectedDelegate <NSObject>
-(void)selectedCountry: (NSInteger)countryReturned;
@end
@property (week, nonatomic) id <CountrySelectedDelegate> myDelegate;
在ViewA上
-(void)selectedCountry: (NSInteger)countryReturned
{
NSLog(@"Country Id is :%d", countryReturned);
}
@interface ViewA : UIViewController < CountrySelectedDelegate >
ViewA
-(void)selectedCountry: (NSInteger)countryReturned
{
NSLog(@"Country Id is :%d", countryReturned);
}
//in ViewControllerB
self.myDelegate = [self parentViewController];
或
如何显示
viewcontrollerB
,作为子视图或显示。?对不起,我忘了显示。立即更新问题首先使viewControllerB成为viewControllerA中的强属性,以便在countrySelect方法完成后不会解除分配。在viewControllerB中使用self.myDelegate=self会发出警告-分配强不兼容类型viewcControllerB*const_strong'将委托设置到该视图控制器,其中你想找回你的价值。谢谢。但是代表现在是零。好的,让我先澄清一下。您在ViewA上,并从要从中提取数据的位置调用ViewB。我说的对吗?对-viewA调用viewB。viewB中的didSelectRowAtIndexPath将解除视图并调用委托。
-(void)selectedCountry: (NSInteger)countryReturned
{
NSLog(@"Country Id is :%d", countryReturned);
}
//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];
}