2013-03-21 80 views
2

我有接收的GeoPoint对象的块内的代码加载的视图控制器:传递一个变量来使用presentViewController

UIViewController *mapViewController1 = 
       [[BSTGMapViewController alloc] initWithNibName:@"BSTGMapViewController_iPhone" 
                  bundle:nil]; 
PFGeoPoint *currentLocation = geoPoint; 
[self presentViewController:mapViewController1 animated:1 completion:nil]; 

我怎样才能GeoPoint的变量传递给mapViewController1所以可以使用它的

viewDidLoad函数?我使用XCode 4.6来定位iOS 5。我该如何解决这个问题?

回答

3

内部BSTGMapViewController超载initWithNibName传递参数

内部BSTGMapViewController。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withGeoPoint:(CLLocation*)userLocation; 

,并实现它在BSTGMapViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withGeoPoint:(CLLocation*)userLocation 
{ 
//usual code of init with nib name 
locationhere=userlocation; 

} 

//我们的代码

PFGeoPoint *currentLocation = geoPoint; 
BSTGMapViewController *mapViewController1 = 
       [[BSTGMapViewController alloc] initWithNibName:@"BSTGMapViewController_iPhone" 
                  bundle:nil withGeoPoint:currentLocation]; 

[self presentViewController:mapViewController1 animated:1 completion:nil]; 
+0

谢谢。正是我想要的。 – Octavian 2013-03-21 13:22:02

+0

欢迎)....... – BhushanVU 2013-03-21 13:30:01

0

在将要呈现的viewController中定义一个属性。财产的类型将是您要通过的类型。

定义属性后,您可以使用您在要显示它的类中创建的对象来访问它。

BSTGMapViewController *mapViewController1 = 
       [[BSTGMapViewController alloc] initWithNibName:@"BSTGMapViewController_iPhone" 
                  bundle:nil]; 
mapViewController1.propertyName = valueToBePassed; 

[self presentViewController:mapViewController1 animated:1 completion:nil]; 

就是这样。您可以在viewDidLoad中访问它。对于BSTGMapViewController东西

0

编写自定义初始化像下面

-(id)initWithGeoPoint:(PFGeoPoint *)point 
{ 
    self = [super initWithNibName:@"BSTGMapViewController_iPhone" 
                  bundle:nil]; 
    if(self) 
    { 
     //here geopoint is class member 
     geopoint = point; 
    } 
} 

并使用

PFGeoPoint *currentLocation = geoPoint; 
UIViewController *mapViewController1 = 
       [[BSTGMapViewController alloc] initWithGeoPoint:currentLocation]; 

希望这有助于。

+0

Vinodh我们需要添加自= [超级initWithNibName:@ “BSTGMapViewController_iPhone” 捆绑:无] ;在自定义初始化程序中。因为它负责设置视图的布局。 – Kunal 2013-03-21 11:38:29

+0

我们正在覆盖默认的初始化程序providie由aple – Vinodh 2013-03-21 12:22:18

0

可以覆盖如下面的代码init方法和新的视图界面中新的视图界面值发送到新的视图控制器

宣言

- (id)initWithTeamCount:(int)teamCount 

定义

- (id)initWithTeamCount:(int)teamCount { 
    self = [super initWithNibName:nil bundle:nil]; 
    if (self) { 
     // Custom initialization 
     self.teamCountLabel.text = [NSString stringWithFormat:@"%d",teamCount]; 
    } 
    return self; 
} 

通价值从第一个角度看

SetTeamsViewController *vc = [[SetTeamsViewController alloc] initWithTeamCount:self.teamCount]; 
0

更改初始化方法:-(id)initWithNubName:(NSString*)nibName bundle:(NSBundle*)nibBundle;到:-(id)initWithNubName:(NSString*)nibName bundle:(NSBundle*)nibBundle location:(PFGeoPoint*)geoLocation;

BSTGMapViewController定义PFGeoPoint*类型的属性currentLocation。在初始化方法中,将该属性设置为参数中的值。

viewDidLoad使用PFGeoPoint*对象的属性值。