Objective c Xcode如何将数据从一个UIviewController传递到另一个UIview子视图

Objective c Xcode如何将数据从一个UIviewController传递到另一个UIview子视图,objective-c,ios6,xcode4.5,Objective C,Ios6,Xcode4.5,我有一个UIViewController(主VC),当点击MainViewController上的按钮时,另一个UIView将作为子视图加载到其中。实际上,我在主视图控制器中将地图视图作为子视图加载。为此,我需要将坐标从MainViewController传递到子视图(地图视图),在那里创建annotation,然后加载到MainViewController中 在MainViewController中,以下方法正确加载子视图: -(IBAction)showUserLocation{ N

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

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
}
在这里,我想将坐标(lat/lon)传递给
UserCurrentLocationView
的类,以便它可以准备地图,并且当按钮调用
showUserLocation
方法时,我可以在
MainViewController
中看到指向的地图

如何从
MainViewController
设置位于
UIView
(子视图)上的属性值


提前谢谢

您必须对
UIView
进行子类化(我称之为
YourView
),并为
long
lat
添加两个属性。然后将其作为名为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
}

做谷歌,你会发现it@AnoopVaidya谷歌很麻烦,但找不到适合我的解决方案:(我所做的是,在subview中创建一个属性并将其合成。然后在主视图类中创建subview类的对象,然后使用此对象设置该属性的数据。但是subview类没有获得从主视图控制器类中设置的值。这是因为它未被保留/strong。或者新实例已被删除。)我不是一个ios开发者,所以不能深入:(这里“currentLocationView”不是我认为的对象。所以,“currentLocationView.fooProperty=self.fooValue”不会work@NuhilMehdy编辑了我的答案。@NuhilMehdy Sweet!(注意,我将上面代码中的变量名“long”(这是一个无效的变量名)编辑为lon。)