亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何傳遞preareForSegue:一個對象

如何傳遞preareForSegue:一個對象

iOS
米琪卡哇伊 2019-06-04 16:03:28
如何傳遞preareForSegue:一個對象我在mapview中有許多注釋(與rightCalloutAccessory(按鈕)。按鈕將從此執行一個segue。mapview轉到tableview..我想通過tableview一個不同的對象(保存數據),這取決于單擊了哪個標注按鈕。例如:(完全是虛構的)注釋1(奧斯汀)->傳遞數據obj 1(與奧斯汀相關)注釋2(達拉斯)->傳遞數據obj 2(與達拉斯相關)注釋3(休斯頓)->傳遞數據obj 3等等.(你知道)我能夠檢測到哪個呼號按鈕被點擊了。我在用prepareForSegue*將數據obj傳遞給目標ViewController..由于我不能讓這個調用為我所需的數據obj增加一個參數,那么有哪些優雅的方法可以達到相同的效果(動態數據obj)?任何提示都將不勝感激。
查看完整描述

3 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

中獲取對目標視圖控制器的引用。prepareForSegue:方法,并將需要的任何對象傳遞到那里。舉個例子.。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }}

修訂:您也可以使用performSegueWithIdentifier:sender:方法來激活基于選擇或按鈕按下的到新視圖的轉換。

例如,假設我有兩個視圖控制器。第一個按鈕包含三個按鈕,第二個按鈕需要知道在轉換之前按下了哪些按鈕。你可以把按鈕連接到IBAction在您的代碼中,它使用performSegueWithIdentifier:方法,像這樣.。

// When any of my buttons are pressed, push the next view- (IBAction)buttonPressed:(id)sender{
    [self performSegueWithIdentifier:@"MySegue" sender:sender];}// This will get called too before the view appears- (void)prepareForSegue:
    (UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {

        // Get destination view
        SecondView *vc = [segue destinationViewController];

        // Get button tag number (or do whatever you need to do here, based on your object
        NSInteger tagIndex = [(UIButton *)sender tag];

        // Pass the information to your destination view
        [vc setSelectedButton:tagIndex];
    }}

編輯:我最初附加的演示應用程序現在已經六年了,所以我刪除了它,以避免任何混淆。


查看完整回答
反對 回復 2019-06-04
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

有時,避免在兩個視圖控制器之間創建編譯時依賴關系是很有幫助的。下面是如何在不關心目標視圖控制器的類型的情況下這樣做:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) {
        [segue.destinationViewController performSelector:@selector(setMyData:) 
                                              withObject:myData];
    } }

因此,只要您的目標視圖控制器聲明了一個公共屬性,例如:

@property (nonatomic, strong) MyData *myData;

您可以在前面的視圖控制器中設置此屬性,就像我前面描述的那樣。


查看完整回答
反對 回復 2019-06-04
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

在SWIFT 4.2中,我會這樣做:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let yourVC = segue.destination as? YourViewController {
        yourVC.yourData = self.someData    }}


查看完整回答
反對 回復 2019-06-04
  • 3 回答
  • 0 關注
  • 671 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號