I have two view controllers: BSViewController
which contains the source ivars number
and array
, and BSotherViewController
which as the target needs to receive the ivars . One way of producing the desired result was provided in this question. The suggestion was to compute the values of self.number
and self.array
in an init
that overrides the designated init
as shown here.
- (id)init {
if (self = [super init]) {
//Set values
NSArray* _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
self.array = _array;
self.number = 25;
}
return self;
}
But I don't know how this solution enables the computation of self.number
or self.array
within the original method (viewDidLoad
); I am only able to get 0 and null for their two values in viewDidLoad
with any approach I have tried.
In addition, the following line produces a warning issue that view
is an unused variable.
BSViewController *view = [[BSViewController alloc] init];
So I am looking for an approach which first computes my ivars number
and array
and then executes the init(WithNumber)
so that the same variables can be used in the target class BSotherViewController
.
I thought a more direct approach would be not use init
, but instead to use something like the following initWithNumber
but I cannot seem to make that work with all the ARC requirements for using underscores, and my limited understanding of objective-c.
- (id)initWithNumber:(NSInteger)number array:(NSArray *)array
{
self = [super init];
if (self) {
_number = number;
_array = array;
return self;
}
return nil;
}
For completeness, I will reproduce below most of the code that was produced in the answer for the previous question.
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
See Question&Answers more detail:
os