iphone - View a 中的 setter 来自 View b : unrecognized selector sent to instance

标签 iphone ios ipad

这是一些代码:

ViewControllerA.h

@interface ViewControllerA: UIViewController {
   NSString *myString;
}

@property (nonatomic,retain)NSString *myString;

ViewControllerA.m

@implementation ViewControllerA
@synthesize myString;

然后我在导航 Controller 中推送 ViewControllerB。当我想返回到 ViewControllerA ViewControllerB.m

-(IBAction)randomAction(id)sender {
   ViewControllerA *myViewController = (ViewControllerA*)[self parentViewController]:
   [myViewController setMyString:@"test"]: // HERE IS THE ERROR : "[DMINavigationController setMyString:]:unrecognized selector sent to instance"
   [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES]
}

为什么?错误返回到“[DMINavigationController setMyString:]”而不是我的 View Controller 是否正常?

最佳答案

我认为您对 ViewController 以及如何在 NavigationController 中正确处理它们有些困惑。

您可能从 DMINavigationController 开始,并使用 ViewControllerA 对其进行初始化。如果您推送一个新的 ViewController ViewControllerB,父级将不是 ViewControllerA,而是 NavigationController。

如果您想访问以前的 ViewController,您需要查询 NavigationController 的 viewControllers 属性来找到它。

你要找的VC在(index of current Object)-1

int index = [self.navigationController.viewControllers indexOfObject:self] - 1;
ViewControllerA *vcA = [self.navigationController.viewControllers objectAtIndex:index];

关于iphone - View a 中的 setter 来自 View b : unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12637133/

相关文章:

iphone - iOS4 上的多任务 : Does it work only on iPhones?

ios - 我是否错误地处理了位置更新?

ios - 如何在 TextView / WebView 中进行左对齐和对齐

html - 滑过图像时图像显示为 'shear'

ios - Xcode 不将 iphone 模拟器显示为目标

在许多机器上进行 iPhone 开发

iphone - 如何获取UITableView中选定的indexPath.section和indexPath.row值?

iphone - 修补 AFJSONRequestOperation 以在运行前检查变量?

ios - 解码 JSON 时在两个结构中使用相同的编码 key

ios - iTunes Connect/Testflight - 请求的应用程序不可用或不存在