第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何傳遞preareForSegue:一個(gè)對(duì)象

如何傳遞preareForSegue:一個(gè)對(duì)象

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

3 回答

?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

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

- (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:方法來(lái)激活基于選擇或按鈕按下的到新視圖的轉(zhuǎn)換。

例如,假設(shè)我有兩個(gè)視圖控制器。第一個(gè)按鈕包含三個(gè)按鈕,第二個(gè)按鈕需要知道在轉(zhuǎn)換之前按下了哪些按鈕。你可以把按鈕連接到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];
    }}

編輯:我最初附加的演示應(yīng)用程序現(xiàn)在已經(jīng)六年了,所以我刪除了它,以避免任何混淆。


查看完整回答
反對(duì) 回復(fù) 2019-06-04
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

有時(shí),避免在兩個(gè)視圖控制器之間創(chuàng)建編譯時(shí)依賴關(guān)系是很有幫助的。下面是如何在不關(guān)心目標(biāo)視圖控制器的類型的情況下這樣做:

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

因此,只要您的目標(biāo)視圖控制器聲明了一個(gè)公共屬性,例如:

@property (nonatomic, strong) MyData *myData;

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


查看完整回答
反對(duì) 回復(fù) 2019-06-04
?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

在SWIFT 4.2中,我會(huì)這樣做:

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


查看完整回答
反對(duì) 回復(fù) 2019-06-04
  • 3 回答
  • 0 關(guān)注
  • 670 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)