按钮单击更新值

时间:2016-05-12 08:01:07

标签: ios objective-c button uialertview

我是IOS新手我需要显示当单击按钮时我的更新数据显示在警报视图中。我的问题双击按钮它只更新我的数据在警报视图但nslog显示正确的方式但我失败的按钮动作。

按钮动作编码:

- (IBAction)buttonaction1:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Amount"
                                                    message:final
                                                   delegate:self
                                          cancelButtonTitle:@"Book Now"
                                          otherButtonTitles: nil];

    alert.tag = 100;
    [alert show];

    amountstr = amountTxt.text;
    NSLog(@"%@",amountstr);
    str = [NSString stringWithFormat:@"type=%@&flag_id=%@&value=%@",typestr,idstr,amountstr];

    [self sendDataToServer :@"POST" params:str];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 100)
    {


         if (buttonIndex == 0)
         {

         }else
         {
         // cancel button pressed
         }
    }
}

Connection确实已完成加载代理:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


    NSError* error111;
    json111 = [NSJSONSerialization JSONObjectWithData: mutableData
                                              options:kNilOptions
                                                error:&error111];


    NSLog(@"%@",json111);
    //NSDictionary *fetchDict = [json111 objectAtIndex:0];
    total = [json111 objectForKey:@"Total"];
    final = [NSString stringWithFormat:@"%@",total];
    NSLog(@"%@",final);
}

传递str字符串的post方法: 编码:

-(void) sendDataToServer : (NSString *) method params:(NSString *)str{

    NSData *postData = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];


    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

    if( theConnection ){

        mutableData = [[NSMutableData alloc]init];
    }
}

2 个答案:

答案 0 :(得分:1)

喜欢

  @interface CreateCatrgeringCustomerViewController : UIViewController<UIAlertViewDelegate>
  {
  NSString *currencyIDe1, *currencyIDe2, *amountType,*BranchIdes;
   NSString *btnSelectType;
   }

在ViewDidload上

 - (void)viewDidLoad
 {
[super viewDidLoad]; 
 // for hold the initial value do like
  BranchIdes = (NSString *)[arrmsg1 objectAtIndex:0];
  currencyIDe1 = (NSString *)[id1 objectAtIndex:0];
   currencyIDe2 = (NSString *)[id2 objectAtIndex:0];

   btnSelectType = @"no";
 }
- (IBAction)buttonaction1:(id)sender {

 btnSelectType = @"yes";

amountstr = amountTxt.text;
NSLog(@"%@",amountstr);
str = [NSString stringWithFormat:@"type=%@&flag_id=%@&value=%@",typestr,idstr,amountstr];

[self sendDataToServer :@"POST" params:str];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


NSError* error111;
json111 = [NSJSONSerialization JSONObjectWithData: mutableData
                                       options:kNilOptions
                                         error:&error111];
NSLog(@"%@",json111);

  if (json111)
  {
    if ([btnSelectType isEqualToString:@"yes"])
    {

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Amount"
                                                message:final
                                               delegate:self
                                      cancelButtonTitle:@"Book Now"
                                      otherButtonTitles: nil];

  alert.tag = 100;
  [alert show];

     }

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 100)
{

  btnSelectType = @"no";

}
}

答案 1 :(得分:0)

使用connectionDidFinishLoading方法而不是操作方法将您的代码警报视图代码变为白色。等,

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
 {


NSError* error111;
json111 = [NSJSONSerialization JSONObjectWithData: mutableData
                                          options:kNilOptions
                                            error:&error111];


NSLog(@"%@",json111);
//NSDictionary *fetchDict = [json111 objectAtIndex:0];
total = [json111 objectForKey:@"Total"];
final = [NSString stringWithFormat:@"%@",total];
NSLog(@"%@",final);

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Amount"
                                                message:final
                                               delegate:self
                                      cancelButtonTitle:@"Book Now"
                                      otherButtonTitles: nil];

alert.tag = 100;
[alert show];
}

更新:

 - (IBAction)buttonaction1:(id)sender {

amountstr = amountTxt.text;
NSLog(@"%@",amountstr);
str = [NSString stringWithFormat:@"type=%@&flag_id=%@&value=%@",typestr,idstr,amountstr];

[self sendDataToServer :@"POST" params:str];

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Amount"
                                                message:final
                                               delegate:self
                                      cancelButtonTitle:@"Book Now"
                                      otherButtonTitles: nil];

alert.tag = 100;
[alert show];


 }