如何管理多个segue?

How to manage multiple segues?

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

但是我只能先显示viewcontroller。

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

 #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

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

为您的按钮制作 IBOutlets button1 和 button2 并为您的按钮制作 IBAction 方法,并将其选择的 属性 设置为 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];
    }
}

希望对您有所帮助。