2013-03-14 111 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

相關問題