如何管理多个segue?

Posted

技术标签:

【中文标题】如何管理多个segue?【英文标题】:How to manage multiple segues? 【发布时间】:2015-12-14 10:39:07 【问题描述】:

我有一个 ViewController,它有两个 segue,我希望这个 segue 在从 URL 获取数据后呈现 viewcontroller。所以我在connectionDidFinishLoading方法中保留了preparesegueWithIdentifier

但我只能显示第一个视图控制器。

我希望两个 segues 都能正常工作。

 #import "specialistquery.h"
    #import "FitnessView.h"
    #import "askCall.h"
    #import "Header.h"

@interface specialistquery ()<UITextFieldDelegate,UITextViewDelegate>

    NSMutableData *responseData;
    UIAlertView *alert;





@end

@implementation specialistquery
@synthesize title,discription,title1,CallBackBtn,SubmitBtn;


- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view.



    dataArray = [[NSArray alloc]initWithObjects:@"Yoga Specialist", @"Dietician", @"Fitness Specialist", @"Homeopathic", @"Ayurvedic Specialist", nil];

    UIPickerView *picker = [[UIPickerView alloc]init];
    picker.delegate=self;
    picker.dataSource=self;
    [picker setShowsSelectionIndicator:YES];
    [self.selectspecs setInputView:picker];



    //toolbar
    UIToolbar *toolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [toolbar setTintColor:[UIColor grayColor]];
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(showSelectedDate)];
    UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolbar setItems:[NSArray arrayWithObjects: space, doneBtn,nil]];
    [self.selectspecs setInputAccessoryView:toolbar];
    [self.title1 setInputAccessoryView:toolbar];
    [self.discription setInputAccessoryView:toolbar];



-(void)showSelectedDate

    [self.selectspecs resignFirstResponder];
    [self.title1 resignFirstResponder];


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if ([segue.identifier isEqualToString:@"good"]) 
        FitnessView *spec = [segue destinationViewController];
        spec.str = _selectspecs.text;
    

    if ([segue.identifier isEqualToString:@"callback"]) 
        askCall *spec = [segue destinationViewController];
        spec.str = _selectspecs.text;
    




- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

    return 1;


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

    return [dataArray count];


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

    return [dataArray objectAtIndex:row];


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

    self.selectspecs.text = [dataArray objectAtIndex:row];




- (IBAction)DisKey:(id)sender 

    [self.selectspecs resignFirstResponder];
    [self.title1 resignFirstResponder];
    [self.discription resignFirstResponder];




-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

    if ([identifier isEqualToString:@"good"])
    
         if ([title1.text isEqualToString:@""] || [discription.text isEqualToString:@""] || [_selectspecs.text isEqualToString:@""])
         
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oppss!!!" message:@"ALL FIELDS MUST BE FILLED" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil ];
             [alert show];
             return NO;
        

        else
        
            NSDate *todayDate = [NSDate date]; // get today date
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
            [dateFormatter setDateFormat:@"dd-MM-yyyy"]; //Here we can set the format which we need
            NSString *convertedDateString = [dateFormatter stringFromDate:todayDate];// here convert date in
            NSString *url;

            url=@"http://www.bha.com/mDru/index.php";
            const char *bytes = [[NSString stringWithFormat:@"question=%@&description=%@&createddate=%@&createdby=iPhone&userid=519&tag=storequestion",title1.text,discription.text,convertedDateString ] UTF8String];
            NSURL *url1 = [NSURL URLWithString:url];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1];

            [request setHTTPMethod:@"POST"];
            [request setHTTPBody:[NSData dataWithBytes:bytes length:strlen(bytes)]];
            [[NSURLConnection alloc] initWithRequest:request delegate:self];


        
    


    else if ([identifier isEqualToString:@"callback"])
    
        if ([title1.text isEqualToString:@""] || [discription.text isEqualToString:@""] || [_selectspecs.text isEqualToString:@""])
        
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oppss!!!" message:@"ALL FIELDS MUST BE FILLED" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil ];
            [alert show];
            return NO;
        

        else
        
            NSDate *todayDate = [NSDate date]; // get today date
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
            [dateFormatter setDateFormat:@"dd-MM-yyyy"]; //Here we can set the format which we need
            NSString *convertedDateString = [dateFormatter stringFromDate:todayDate];// here convert date in
            NSString *url;

            url=@"http://www.bha.com/mDru/index.php";
            const char *bytes = [[NSString stringWithFormat:@"question=%@&description=%@&createddate=%@&createdby=iPhone&userid=519&tag=storequestion",title1.text,discription.text,convertedDateString ] UTF8String];
            NSURL *url1 = [NSURL URLWithString:url];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1];

            [request setHTTPMethod:@"POST"];
            [request setHTTPBody:[NSData dataWithBytes:bytes length:strlen(bytes)]];
            [[NSURLConnection alloc] initWithRequest:request delegate:self];


        

    


    return NO;





-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

    responseData = [[NSMutableData alloc] init];


    alert = [[UIAlertView alloc] initWithTitle:@"Please Wait..." message:@"Saving Data" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alert show];


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    [responseData appendData:data];

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

    [alert dismissWithClickedButtonIndex:0 animated:YES];

    UIAlertView *nsjsdn = [[UIAlertView alloc] initWithTitle:nil message:@"Internet connection problem" delegate:self cancelButtonTitle:@"Exit" otherButtonTitles:nil, nil];
    [nsjsdn show];


- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

    NSString *teststr=[[NSString alloc]  initWithData:responseData encoding:NSUTF8StringEncoding];

    NSString *str1234 = [teststr stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSLog(@"Response is %@ %lul",str1234,(unsigned long)
          [str1234 length]);


    [alert dismissWithClickedButtonIndex:0 animated:YES];


    [self performSegueWithIdentifier:@"good" sender:self];

    [self performSegueWithIdentifier:@"callback" sender:self];



@end

【问题讨论】:

两个segues触发意味着什么?你在设计什么样的用户界面? 正如@Avi 提到的,为什么要同时导航到两个视图控制器?错了 你应该知道代理方法shouldPerformSegueWithIdentifier :只有在从UI本身启动segue时才会被调用,这意味着IB中的segue链接,因为视图控制假定当你调用performSegue时你知道你在做什么,因此跳过这部分。只是想指出。 @DilumN 我想一次只分割一个视图,但在获取数据之后。两个按钮都有不同的 segue 和不同的视图,但功能是相同的。我还尝试了 if-else 连接确实完成了加载,但它提示错误“ undeclare identifier 'identifier'。 @M.Porooshani 我已经从 IB 发起了 segue,但我想在数据获取完成后进行 segue。 【参考方案1】:

如果你这样做,我相信你应该得到你需要的结果。

为您的按钮制作 IBOutlets button1 和 button2,为您的按钮制作 IBAction 方法,并将其 selected 属性设置为 YES。

- (IBAction)button1Clicked:(id)sender

    button1.selected = YES;
    if (button2.selected)
    
        // This is done so that both buttons do not appear as clicked at the same time
        button2.selected = NO;
    


- (IBAction)button2Clicked:(id)sender

    button2.selected = YES;
    if (button1.selected)
    
        // This is done so that both buttons do not appear as clicked at the same time
        button1.selected = NO;
    

完成此操作后,在您的 connectionDidFinishLoading 方法中执行此检查。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

    NSString *teststr=[[NSString alloc]  initWithData:responseData encoding:NSUTF8StringEncoding];

    NSString *str1234 = [teststr stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSLog(@"Response is %@ %lul",str1234,(unsigned long)
      [str1234 length]);


    [alert dismissWithClickedButtonIndex:0 animated:YES];

    if (button1.selected)
    
        [self performSegueWithIdentifier:@"good" sender:self];
    
    else if (button2.selected)
    
        [self performSegueWithIdentifier:@"callback" sender:self];
    

希望对你有帮助。

【讨论】:

它适用于我,但现在当我点击它时按钮变成蓝色......有什么办法让它不会变成蓝色。 您的按钮类型是“系统”吗?将按钮类型设置为“自定义”。可能是这样 请回答我今天问的另一个问题。

以上是关于如何管理多个segue?的主要内容,如果未能解决你的问题,请参考以下文章

如何从一个 viewController 添加多个 segue

无法更改为具有多个 segue 的另一个视图控制器。我该如何解决?

如何通过来自同一个 TableViewController 的多个 segue 到同一个 ViewController

多个视图控制器 segue 并快速执行功能

uitableview中的多个segues

在 UITableView 上执行具有多个状态的 segue 的问题