首先,我已经尝试在线搜索解决方案,但没有一个对我有用,我在想,因为我正在使用 ECSlidingViewController 在应用程序中导航,所以我无法使用 prepareForSegue
方法因此,我的问题可能需要不同的方法。
我有一个名为
viewInits
的类,它在.h
文件中包含我希望允许其他类设置和获取其值的属性。在这种情况下,该属性是一个 NSString*navBarTitle
。在
ClassA
中,我有一个tableView:didSelectRowAtIndexPath:
方法,其中我- 创建一个 ViewInits 类对象 -
*viewInits
。 - 然后我将
setNavBarTitle:
设置为[self.MenuRowsArray objectAtIndex:indexPath.row]
的值。 - 在下一行中,我做了一个 NSLog 来检查,是的,
viewInits.navBarTitle
现在拥有我想要的值。
- 创建一个 ViewInits 类对象 -
在
ClassB 的
viewDidloadMethod
中,类似地,我创建了一个ViewInits
对象 -* viewInits
并对viewInits.navBarTitle
进行了 NSLog 检查。但它返回(null)
。 这似乎是什么问题?
这是我尝试传递 NSString 的代码。我做错了什么?
viewInit.h
@interface ViewInits : NSObject
@property (strong, nonatomic) NSString *navBarTitle;
@end
ClassA.m
tableView:didSelectRowAtIndexPath: 方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [self.MenuRowsArray objectAtIndex:indexPath.row];
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
// *---------- Assign identifier to NSString viewInits ----------*
ViewInits *viewInits = [[ViewInits alloc] init];
[viewInits setNavBarTitle:identifier];
NSLog(@"%@", viewInits.navBarTitle);
// *---------- Assign identifier to NSString viewInits ----------*
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^
{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
}];
}
ClassB.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// *========== ECSlidingViewController ==========*
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]])
{
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
// *========== ECSlidingViewController ==========*
ViewInits *viewInits = [[ViewInits alloc] init]; // Create ViewInit class object
self.navBar.topItem.title = viewInits.navBarTitle;
NSLog(@"%@", viewInits.navBarTitle); // <<--- This always ends up null. What's wrong?
}
非常感谢您的帮助。谢谢。
最佳答案
如果您想将 ViewInit
用作设置的公共(public)存储,它应该是一个单例,以便应用程序中的所有其他实例都可以获取它。当前,您每次要使用它时都在创建一个新实例,因此新实例没有您之前的任何设置。
另外,我知道滑动 View Controller 是什么,我问它是因为您可能使用不当。如果你有一个 View Controller ,它是当前的顶 View Controller 并且它改变了顶 View Controller (类 A 可能正在这样做,不确定)那么引用 self.slidingViewController
将停止工作中途你的代码。
关于ios - 将 NSString 从一个类传递到另一个类。 (ECSlidingViewController?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17916399/