簡體   English   中英

是否可以從另一個類調用performSegueWithIdentifier?

[英]Is it possible to call a performSegueWithIdentifier from another class?

我只是建立一個類來正確管理我的數據庫和JSON請求。 現在的問題是,我該如何執行搜尋?

這是我認為的代碼:

- (IBAction)loginClick:(id)sender
{
    NSString *post = [NSString stringWithFormat:@"username=test&password=test"];
    [[DataManagement sharedManager] WebServiceLogin:post];
}
- (void) showTypeView
{
    [self performSegueWithIdentifier:@"showTypeView" sender:nil];
}

在我的課上:

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
...
                switch ([[response valueForKey:@"success"] intValue])
                {
                    case 0:
                    {
                        NSLog(@"error: %@ error Description: %@", [response valueForKey:@"success"], [response valueForKey:@"error_message"]);
                        break;
                    }
                    case 1:
                    {
                        LoginViewController *showView = [LoginViewController new];
                        [showView showTypeView];
                        break;
                    }
                    default:
                        break;
                }
...
    }

啟動時出現錯誤:**

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<LoginViewController: 0x165afd30>) has no segue with identifier 'showTypeView''
*** First throw call stack:
(0x2592e2eb 0x250fadff 0x29e2b037 0xe1819 0xdb64f 0x25f64de1 0x25f64d99 0x25f64e8d 0x25e261ef 0x25edf04f 0xa77cab 0xa7f835 0x25e171e3 0x258415f9 0x25e170cb 0x25e16f95 0x25e16e29 0x258f1257 0x258f0e47 0x258ef1af 0x25841bb9 0x258419ad 0x26abbaf9 0x29b2dfb5 0xe3ea9 0x254f4873)
libc++abi.dylib: terminating with uncaught exception of type NSException

**

如果您使用的是segueWithIdentifier,則需要已經在Storyboard中內置了segue,並將其正確標記為“ showTypeView”。 否則,您應該使用導航控制器來推送視圖控制器,或者使用self presentViewController來顯示模式視圖控制器。

編輯:基於Larme的注釋,您可以像這樣構建一個委托:

// In your class.h file
@property (weak, nonatomic)id<SegueDelegate> delegate;

// In class.m file
LoginViewController *showView = [LoginViewController new];
self.delegate = showView;
[self.delegate segue];


// In LoginViewController.h
@protocol SegueDelegate
-(void)segue;
@end

@interface LoginViewController: UIViewController <SegueDelegate>
-(void)segue;
@end

// In LoginViewController.m
@implementation LoginViewController
-(void)segue
{
    [self performSegueWithIdentifier:@"showTypeView" sender:nil];
}
@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM