我在GroupView.h中定义了变量
@interface GroupView()
{
NSMutableArray *chatrooms;
}
@end
@implementation GroupView
现在我想在segue中传递这个变量
@interface FriendsViewController ()
@end
@implementation FriendsViewController
else if ([segue.identifier isEqualToString:@"showGroupView"]) {
GroupView *groupView = (GroupView *)segue.destinationViewController;
groupView.chatrooms = [NSMutableArray arrayWithArray:chatrooms];
}
我知道聊天室必须是头文件中的属性才能以这种方式编码,但它不是
那么有没有办法在segue中使用这个变量。
感谢您的帮助。
答案 0 :(得分:1)
chatrooms
定义为您已完成的ivar,使用->
表示法进行访问:
groupView->chatrooms = [NSMutableArray arrayWithArray:chatrooms]
但是,这通常是不鼓励的。您应该使用属性:
@interface GroupView
@property (strong) NSMutableArray *chatrooms;
@end
顺便提一下,如果您使用的是NSMutableArray
,则表示您想要直接修改数组的元素列表,而不仅仅是替换数组批发。如果你每次只想用一个全新的数组替换数组,我建议改为使用NSArray
。
此处要做的另一点是,您试图将segue.destinationViewController
处的对象转换为GroupView
。您已经以非常误导的方式命名了UIViewController
子类,或者您没有访问GroupView
作为返回给您的UIViewController
的正确成员。
答案 1 :(得分:1)
通常情况下,如果您没有构建SDK或其他东西。你没有更好的理由不在头文件中公开它。但是,您可以在扩展中公开该属性并在宿主类中声明一个私有属性(如果您只是声明一个局部变量,它实际上无法传递)。例如,您有一个名为GroupView+Helper
的扩展程序。因此,您可以将其传递到扩展中公开的属性中。然后在内部转换为GroupView
。
在 GroupView.m :
中@interface GroupView
@property (strong, nonatomic) NSMutableArray *chatrooms;
@end
在 GroupView + Helper.h
中@property (strong, nonatomic) NSMutableArray *internalChatrooms;
此外,您需要在 GroupView 中导入 GroupView + Helper 。
它会使您的chatrooms
私人和内部聊天室受到保护。