我有两个视图控制器:BSViewController,它包含源ivars number和array,以及作为目标需要接收ivars的BSotherViewController。One way of producing the desired result was provided in this question.建议在覆盖指定init的init中计算self.number和self.array的值,如下所示。
- (id)init {
if (self = [super init]) {
//Set values
NSArray* _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
self.array = _array;
self.number = 25;
}
return self;
}但是我不知道这个解决方案如何在原始方法(viewDidLoad)中实现self.number或self.array的计算;在我尝试过的任何方法中,我只能在viewDidLoad中为它们的两个值获得0和null。
此外,下面的行将生成一个警告问题,指出view是一个未使用的变量。
BSViewController *view = [[BSViewController alloc] init];因此,我正在寻找一种方法,它首先计算我的ivar、number和array,然后执行init(WithNumber),以便在目标类BSotherViewController中使用相同的变量。
我认为一种更直接的方法不是使用init,而是使用类似下面的initWithNumber,但我似乎无法满足所有使用下划线的ARC要求,以及我对objective-c的有限理解。
- (id)initWithNumber:(NSInteger)number array:(NSArray *)array
{
self = [super init];
if (self) {
_number = number;
_array = array;
return self;
}
return nil;
}为了完整起见,我将在下面重现上一个问题的答案中生成的大部分代码。
BSViewController.h
#import <UIKit/UIKit.h>
@interface BSViewController : UIViewController{
// NSInteger number;
}
@property (nonatomic) NSInteger number;
@property (nonatomic, weak) NSArray * array;
// - (id)initWithNumber:(NSInteger)number array:(NSArray *)array;
@endBSViewController.m
#import "BSViewController.h"
@interface BSViewController ()
@end
@implementation BSViewController
@synthesize number;
@synthesize array;
/*
- (id)initWithNumber:(NSInteger)number array:(NSArray *)array
{
self = [super init];
if (self) {
_number = number;
_array = array;
return self;
}
return nil;
}
*/
- (id)init {
if (self = [super init]) {
NSArray* _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
self.array = _array;
self.number = 25;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"self: %@", self);
BSViewController *view = [[BSViewController alloc] init]; //Warning issued: unused variable
NSLog(@"self number: %d", self.number);
NSLog(@"self array: %@", self.array);
}
@endBSotherViewController.h
#import <UIKit/UIKit.h>
@class BSViewController;
@interface BSotherViewController : UIViewController
@property (strong, nonatomic) BSViewController *aview;
@endBSotherViewController.m
#import "BSotherViewController.h"
#include "BSViewController.h"
@interface BSotherViewController ()
@end
@implementation BSotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
BSViewController *aview = [[BSViewController alloc] init];
NSLog(@"other view: %@", self.aview);
NSLog(@"other number: %d", aview.number);
NSLog(@"other array: %@", aview.array);
}
@end发布于 2013-03-13 06:06:41
你使用的是一个故事板片段。segue将通过从故事板加载来创建目标视图控制器。创建-[BSotherViewController initWithNumber:array:]方法是没有意义的,因为段不会使用它。
当用户通过点击按钮触发segue时,系统会创建一个UIStoryboardSegue实例。这个片段对象有一个destinationViewController属性,它(在本例中)将是即将出现的BSotherViewController实例。系统向源代码视图控制器(您的BSViewController)发送prepareForSegue:sender:消息,并将segue对象作为第一个参数进行传递。
您需要在BSViewController中实现prepareForSegue:sender:。在该方法中,您可以访问源视图控制器(self)和目标视图控制器(segue.destinationViewController),因此可以将数据从一个控制器传递到另一个控制器。
首先,将number和array属性添加到BSotherViewController。然后,将如下方法添加到BSViewController中
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// The identifier here must match the one you assigned to the segue in the storyboard.
if ([segue.identifier isEqualToString:@"GoingToOtherViewController"]) {
BSotherViewController *destination = segue.destinationViewController;
destination.number = self.number;
destination.array = self.array;
}
}最后,在-[BSotherViewController viewDidLoad]中,使用新的number和array属性的值来设置视图的内容。
https://stackoverflow.com/questions/15372796
复制相似问题