2013-03-12 23 views
0

我有兩個視圖控制器:BSViewController其中包含源ivars numberarrayBSotherViewController,它們作爲目標需要接收ivars。 One way of producing the desired result was provided in this question.建議計算init中的self.numberself.array的值,該值覆蓋指定的init,如下所示。將ivars放入init

- (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.numberself.array計算;我只能通過我嘗試過的任何方法在viewDidLoad中獲得0和null兩個值。

此外,以下行會產生警告問題,view是未使用的變量。

BSViewController *view = [[BSViewController alloc] init]; 

所以我要尋找它首先計算實例變量我和numberarray方式,然後執行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; 
@end 

BSViewController.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); 
} 
@end 

BSotherViewController.h

#import <UIKit/UIKit.h> 
@class BSViewController; 
@interface BSotherViewController : UIViewController 
@property (strong, nonatomic) BSViewController *aview; 
@end 

BSotherViewController.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 
+0

「BSViewController」的指定初始值設定項是'initWithNibName:bundle:',所以你覆蓋它而不是'init'。 – mipadi 2013-03-12 21:47:25

+0

在你的另一個問題中,你說「'BSViewController'上有一個按鈕,它是連接到'BSotherViewController'」。你的意思是說你正在使用一個故事板並且已經從按鈕創建了一個segue到一個'BSotherViewController'? – 2013-03-12 21:48:03

+0

是的,我正在使用故事板。 Wrt mipadi的評論:這會讓我更接近我想要的答案嗎?我認爲我真的應該使用「模型」類而不是「控制器」類,但在這一點上,我只是試圖讓某種測試工作。我的兩個變量'number'和'array'在VC中計算並用於另一個VC中。我只是想弄清楚從前者到後者的物流。我不知道我會有這麼多麻煩。 – zerowords 2013-03-12 21:57:09

回答

1

您正在使用故事板賽格。 segue將通過從故事板中加載它來創建目標視圖控制器。創建-[BSotherViewController initWithNumber:array:]方法沒有意義,因爲segue不會使用它。

當用戶通過點擊按鈕來觸發分段時,系統會創建一個UIStoryboardSegue的實例。這個segue對象有一個destinationViewController屬性,它將(在你的情況下)是即將出現的BSotherViewController實例。系統向源視圖控制器(您的BSViewController)發送prepareForSegue:sender:消息,並將segue對象作爲第一個參數傳遞。

您需要執行prepareForSegue:sender:BSViewController。在該方法中,您可以訪問源視圖控制器(self)和目標視圖控制器(segue.destinationViewController),因此您可以將數據從一個傳遞到另一個。

首先,將numberarray屬性添加到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],使用新的numberarray屬性的值來設置你的意見的內容。

+0

你的答案中的第一段讓我感到困惑:我沒有想到在BSotherViewController中創建' - [BSotherViewController initWithNumber:array:]',但是在'BSViewController'中。我錯過了什麼嗎?不過,我仍在消化你的答案。謝謝。 – zerowords 2013-03-12 22:16:52

+0

然後我誤解了你的計劃。我不明白你爲什麼要把這個方法放在'BSViewController'上,因爲(我認爲)''BSViewController'正從你的故事板加載,故事板加載器將不會使用你自定義的'initWithNumber:array:'方法。 – 2013-03-12 22:23:20

+0

我試着實現你爲segue做的準備,但是我沒有得到任何新的錯誤,但是我沒有從BSViewController中獲取信息到BSotherViewController。在我的真實應用程序中,我從故事板獲得了一個URL,其中包含我在「BSViewController」中解析並放入「數組」並計算「數字」的文本。但是,在BSotherViewController中,用戶反應也需要結果。 – zerowords 2013-03-12 22:32:50