2013-03-14 112 views
0

我有一个UIViewController(主VC),当在MainViewController上点击一个按钮时,另一个UIView作为子视图加载到其中。其实我在主视图控制器中加载一个地图视图作为子视图。为此,我需要将MainViewController的坐标传递给子视图(地图视图)以创建annotation那里&然后加载到MainViewController的内部。Xcode如何将数据从一个UIviewController传递到另一个UIview子视图

MainViewController以下方法加载正确的子视图:

-(IBAction)showUserLocation{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"UserCurrentLocationView" owner:self options:nil]; 
    UIView *currentLocationView = [nib objectAtIndex:0]; 

    currentLocationView.frame = CGRectMake(8, 10, currentLocationView.frame.size.width, currentLocationView.frame.size.height); 

    [thirdPortion addSubview:currentLocationView]; // thirdPortion is a View Outlet in the Main VC wh 
} 

在这里,我想传递一个坐标(纬度/经度)的UserCurrentLocationView的类,以便它可以准备在地图上,我可以看到在MainViewController的指针图showUserLocation方法被一个按钮调用。

什么是设置保留(子视图)从MainViewController属性的值的方法。

在此先感谢!

+0

做谷歌,你会发现它 – 2013-03-14 10:13:13

+1

@AnoopVaidya懒得做了谷歌,但找不到对我来说:( 工作我做的是,在子视图和合成产生的财产吧。然后创建一个解决方案子视图类的对象,然后使用此对象设置该属性的数据,但子视图类没有得到从主视图控制器类设置的值 – 2013-03-14 10:35:01

+0

这是因为它不是保留/强。或者一个新实例正在创建。我不是一个ios开发人员,所以不能深入:( – 2013-03-14 10:39:06

回答

0

您必须继承UIView(我将其称为YourView)并为longlat添加两个属性。然后将其作为名为UserCurrentLocationView的.xib文件的类。

然后,您执行以下操作来设置变量。

-(IBAction)showUserLocation{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"UserCurrentLocationView" owner:self options:nil]; 
    YourView *currentLocationView = (YourView *)[nib objectAtIndex:0]; 

    currentLocationView.lon = self.fooValue; 
    currentLocationView.lat = self.fooValue2; 

    currentLocationView.frame = CGRectMake(8, 10, currentLocationView.frame.size.width, currentLocationView.frame.size.height); 

    [thirdPortion addSubview:currentLocationView]; // thirdPortion is a View Outlet in the Main VC wh 
} 
+0

这里“currentLocationView”不是我想的对象。所以,“currentLocationView.fooProperty = self.fooValue”wont工作 – 2013-03-14 11:10:08

+0

@NuhilMehdy编辑我的答案。 – Groot 2013-03-14 11:22:51

+0

非常感谢。这样可行 !!! – 2013-03-14 11:38:52

相关问题