iOS导航下一个ViewController

时间:2015-05-14 09:45:21

标签: ios objective-c

我有一个名为PlantingTreeVC的视图控制器。因为那里有两种类型的支付选项,即。 PaybyCreditCardPaybyCash。如果用户选择PaybyCreditCard并点击NextButton,则应导航至PayPalVC,或者如果用户选择PaybyCash,则点击NextButton应该导航到CustomerDetailsVC。我怎么能这样做?

我的代码如下:

PlantingTreeVC.h

#import "ViewController.h"

@interface PlantingTreeVC : ViewController {
    BOOL checked;
}
@property (weak, nonatomic) IBOutlet UIImageView *topStrip;
@property (weak, nonatomic) IBOutlet UIImageView *bgView;
@property (weak, nonatomic) IBOutlet UITextField *plantsNumber;
@property (weak, nonatomic) IBOutlet UIImageView *AddNamesBG;
@property (weak, nonatomic) IBOutlet UILabel *plantNameLbl;
@property (weak, nonatomic) IBOutlet UITextField *nametextField;
@property (weak, nonatomic) IBOutlet UILabel *commaTextLbl;
@property (weak, nonatomic) IBOutlet UILabel *getPlaqueLbl;

@property (weak, nonatomic) IBOutlet UIScrollView *plantScrollView;
@property (weak, nonatomic) IBOutlet UISwitch *Switch;

@property (weak, nonatomic) IBOutlet UIImageView *giveTreeView;
@property (weak, nonatomic) IBOutlet UISwitch *giveTreeIB;
@property (weak, nonatomic) IBOutlet UILabel *giveTreeLbl;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property (weak, nonatomic) IBOutlet UILabel *phonenoLabel;
@property (weak, nonatomic) IBOutlet UITextField *friendnameTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *phonenoTextField;
@property (weak, nonatomic) IBOutlet UILabel *totalAmountLabel;
@property (weak, nonatomic) IBOutlet UILabel *amountTextLbl;
@property (weak, nonatomic) IBOutlet UIImageView *paymentView;
@property (weak, nonatomic) IBOutlet UILabel *payByCardLbl;
@property (weak, nonatomic) IBOutlet UILabel *payByCashLbl;

@property (weak, nonatomic) IBOutlet UIButton *paybyCardIB;
@property (weak, nonatomic) IBOutlet UIButton *payByCashIB;
@property (weak, nonatomic) IBOutlet UIButton *backBtIB;
@property (weak, nonatomic) IBOutlet UIButton *nextBtIB;
- (IBAction)BackButton:(id)sender;
- (IBAction)NextButton:(id)sender;

- (IBAction)PayByCardButton:(id)sender;
- (IBAction)PayByCashButton:(id)sender;

- (IBAction)NamesSwitchController:(id)sender;

- (IBAction)GivethisTreeController:(id)sender;
@end

PlantingTreeVC.m

#import "PlantingTreeVC.h"

#import "PayPalVC.h"

@interface PlantingTreeVC ()



@end



@implementation PlantingTreeVC

@synthesize plantsNumber;

@synthesize AddNamesBG;

@synthesize Switch;

@synthesize plantNameLbl;

@synthesize nametextField;

@synthesize commaTextLbl;

@synthesize plantScrollView;

@synthesize bgView;

@synthesize topStrip;

@synthesize backBtIB;

@synthesize nextBtIB;



@synthesize giveTreeView;

@synthesize giveTreeIB;

@synthesize nameLabel;

@synthesize emailLabel;

@synthesize phonenoLabel;

@synthesize friendnameTextField;

@synthesize emailTextField;

@synthesize phonenoTextField;

@synthesize paybyCardIB;

@synthesize payByCashIB;



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

    [super touchesBegan:touches withEvent:event];

}



- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view sendSubviewToBack:bgView];

    [self.view bringSubviewToFront:topStrip];

    [self.view bringSubviewToFront:plantScrollView];

    [self.view bringSubviewToFront:backBtIB];

    [self.view bringSubviewToFront:nextBtIB];





    nametextField.backgroundColor = [UIColor clearColor];

    friendnameTextField.backgroundColor = [UIColor clearColor];

    emailTextField.backgroundColor = [UIColor clearColor];

    phonenoTextField.backgroundColor = [UIColor clearColor];

    plantNameLbl.hidden =YES;

    nametextField.hidden = YES;

    commaTextLbl.hidden=YES;



    nameLabel.hidden=YES;

    emailLabel.hidden =YES;

    phonenoLabel.hidden=YES;

    friendnameTextField.hidden=YES;

    emailTextField.hidden=YES;

    phonenoTextField.hidden=YES;



    checked=NO;





    [plantScrollView setScrollEnabled:YES];



    CGFloat scrollViewHeight = 0.0f;

    for (UIView* view in plantScrollView.subviews)

    {

        scrollViewHeight += view.frame.size.height;

    }

    [plantScrollView setContentSize:(CGSizeMake(300, 900))];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)BackButton:(id)sender {

}



- (IBAction)NextButton:(id)sender {

    NSLog(@"noofplants:%@",plantsNumber.text);

    NSLog(@"plant_names:%@",nametextField.text);

   // NSLog(@"name:%@",namet.text);

    //NSLog(@"email:%@",emailTextField.text);

   // NSLog(@"phone_number:%@",phonenoTextField.text);

    NSLog(@"friend_name:%@",friendnameTextField.text);

    NSLog(@"friend_email:%@",emailTextField.text);

    NSLog(@"f_phone_number:%@",phonenoTextField.text);

 //   NSLog(@"pickup_address:%@",passwordTextField.text);

 //   NSLog(@"purchase_date:%@",confirmTextField.text);

 //   NSLog(@"renewal_date:%@",passwordTextField.text);

   // NSLog(@"price:%@",);

    //NSLog(@"price:%@",payment_method_type.text);





    /* if ([self isValidEmail:[emailTextField text]] == 1) {



     UIAlertView* alertViewq = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please enter Valid email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

     [alertViewq show];

     } */



    if([plantsNumber.text isEqualToString:@""] || [nametextField.text isEqualToString:@""] || [friendnameTextField.text isEqualToString:@""] || [emailTextField.text isEqualToString:@""] || [phonenoTextField.text isEqualToString:@""])



    {

        UIAlertView* alertViewq = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please enter all the details." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertViewq show];



    }

      else

    {

        NSString *post = [NSString stringWithFormat:@"&noofplants=%@&plant_names=%@&friend_name=%@&friend_email=%@&f_phone_number=%@",plantsNumber.text,nametextField.text,friendnameTextField.text,emailTextField.text,phonenoTextField.text];

        NSLog(@"post:%@",post);

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://someurl.com/sample.php"]]];

        [request setHTTPMethod:@"POST"];

        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

        [request setHTTPBody:postData];

        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];



        if (theConnection)

        {

        }

        else

        {

            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Check your networking configuration." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [alertView show];

        }



    }

}



- (IBAction)PayByCardButton:(id)sender {

   }



- (IBAction)PayByCashButton:(id)sender {

    if (!checked) {

        [payByCashIB setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];

        checked=YES;

    }

    else if (payByCashIB){

        [payByCashIB setImage:[UIImage imageNamed:@"uncheckbox.png"] forState:UIControlStateNormal];

        checked=NO;

    }

//    

//      PayPalVC *PVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PaymentScreen"];

//     [self presentViewController:PVC animated:YES completion:nil];

}



- (IBAction)NamesSwitchController:(id)sender {

    if (Switch.on) {



        self.AddNamesBG.frame = CGRectMake(5, 79, 288,156);

        plantNameLbl.hidden =NO;

        nametextField.hidden = NO;

        commaTextLbl.hidden=NO;

}



    else {

        self.AddNamesBG.frame = CGRectMake(5, 79, 288,61);

        plantNameLbl.hidden =YES;

        nametextField.hidden = YES;

        commaTextLbl.hidden=YES;

 }

}

- (IBAction)GivethisTreeController:(id)sender {

    if (giveTreeIB.on) {



        self.giveTreeView.frame = CGRectMake(5, 232, 288,210);

        nameLabel.hidden=NO;

        emailLabel.hidden =NO;

        phonenoLabel.hidden=NO;

        friendnameTextField.hidden=NO;

        emailTextField.hidden=NO;

        phonenoTextField.hidden=NO;

}



    else {

        self.giveTreeView.frame = CGRectMake(5, 232, 288,64);

        nameLabel.hidden=YES;

        emailLabel.hidden =YES;

        phonenoLabel.hidden=YES;

        friendnameTextField.hidden=YES;

        emailTextField.hidden=YES;

        phonenoTextField.hidden=YES;

    }

}

@end

1 个答案:

答案 0 :(得分:2)

@interface PlantingTreeVC ()
{
   PaybyCreditCard_BOOL,PaybyCash_BOOL
}
@end

  //Other code

- (IBAction)PayByCardButton:(id)sender{
    if(PaybyCreditCard_BOOL){
      PaybyCreditCard_BOOL=NO;
     }
     else{
      PaybyCreditCard_BOOL=YES;

    //Dont forget this check also
         if(PaybyCash_BOOL){
            PaybyCash_BOOL =NO;
          }


      }

  }

- (IBAction)PayByCashButton:(id)sender{
   if(PaybyCash_BOOL){
    PaybyCash_BOOL =NO;
     }
   else{
   PaybyCash_BOOL =YES;
 //Dont forget this check also

         if(PaybyCreditCard_BOOL){
             PaybyCreditCard_BOOL =NO;
          }
    }
  }

 - (IBAction)NextButton:(id)sender{
     if(PaybyCreditCard_BOOL){
     //go toPayPalVC
     }
    else if(PaybyCash_BOOL){

    //go toPayPalCash
    }
    else{
      }
  }

创建两个bool PaybyCreditCard_BOOL,PaybyCash_BOOL,默认设置为NO。当点击按钮时切换为是和否。参见上面的代码