如何在ios应用程序中设置按钮启用和禁用?

时间:2014-11-13 14:47:38

标签: ios objective-c

我正在创建2个按钮,一个用于喜欢,一个用于不喜欢,当我点击按钮时禁用它并同时禁用不喜欢按钮等等。当我第二次运行应用程序时,它不会改变。我已经实现了它的工作,但问题是,当我再次运行我的应用程序时,按钮启用。问题是我不希望那个按钮同一个按钮enable.please帮我解决我的代码中的问题

    - (void)viewDidLoad
       {
        [super viewDidLoad];
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:102/255.0 
        green:102/255.0 blue:204/255.0 alpha:1.0];
        self.navigationController.navigationBar.titleTextAttributes = 
        @{NSForegroundColorAttributeName : [UIColor whiteColor]};

        smsdisplaytext.editable=NO;

        smsdisplaytext.backgroundColor= [UIColor colorWithRed:102/255.0 green:102/255.0 
        blue:204/255.0 alpha:1.0];
        self.navigationItem.title=@"Insta SMS";

         [ self getSmsData];
         [self smsdisplay];
         [self getLike];
         }




            -(void)sendlike
          {

           NSURL *url = [NSURL URLWithString: 
           @"http://sms.instatalkcommunications.com/apireq/AddRatingForSMS"];

           ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

           [request setPostValue:[NSString stringWithFormat:@"%d",1] forKey:@"t"];

            [request setPostValue:@"admin" forKey:@"h"];
             [request setPostValue:[NSString stringWithFormat:@"%@",self.Id] forKey:@"cid"];
           [request setPostValue:[NSString stringWithFormat:@"%d",1234567890] forKey:@"token"];
            [request setPostValue:@"test@test.com" forKey:@"email"];
            [request setTag:2];


            [request setPostValue:@"true" forKey:@"like"];


            [request setDelegate:self];
            [request startAsynchronous];

               }
           -(void)senddislike
          {
        NSURL *url = [NSURL URLWithString:
        @"http://sms.instatalkcommunications.com/apireq/AddRatingForSMS"];

       ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

      [request setPostValue:[NSString stringWithFormat:@"%d",1] forKey:@"t"];

      [request s  etPostValue:@"admin" forKey:@"h"];
      [request setPostValue:[NSString stringWithFormat:@"%@",self.Id] forKey:@"cid"];
      [request setPostValue:[NSString stringWithFormat:@"%d",1234567890] forKey:@"token"];
      [request setPostValue:@"test@test.com" forKey:@"email"];
      [request setPostValue:@"false" forKey:@"Like"];
      [request setTag:3];

      [request setDelegate:self];
      [request startAsynchronous];

      }
      - (void) requestFinished:(ASIHTTPRequest *)request
    {
      NSString *responseString = [request responseString];
     responseString = [request responseString];
     NSLog(@"%@",responseString);
    if(request.tag==1)
     {
    SBJsonParser *parser = [[SBJsonParser alloc] init] ;

    NSArray *jsondata = [parser objectWithString:responseString];

        NSLog(@"%@",jsondata);
    for (NSObject* item in jsondata)
    {
        likelabel.text = [[item valueForKey:@"Liked"] stringValue];
        dislikelabel.text = [[item valueForKey:@"Disliked"] stringValue];
    }


      }
   else if(request.tag==2)
    {
    [self getLike];

     }
   ///////  getting comment

   else if(request.tag==4)
    {
        // get comments

        SBJsonParser *parser = [[SBJsonParser alloc] init] ;

        NSMutableArray *jsondata = [parser objectWithString:responseString];


        for(int i=0;i<jsondata.count;i++)
        {
            NSObject *temp = [jsondata objectAtIndex:i];


            NSMutableDictionary *message = [[NSMutableDictionary alloc] init];



            message[kMessageContent]=[temp valueForKey:@"Comment"];
            message[@"Timestamp"]=[self dateWithJSONString:[temp valueForKey:@"CreatedDate"]];

            last=[[temp valueForKey:@"Id"] integerValue];

            [_chatController addNewMessage:message];


            }
      }
     else
     {
        ////post comment
        [self getComment];
    }

    }

//这种获取喜欢和不喜欢的方法

  -(void)getLike
   {

   NSString *url = [NSString 
   stringWithFormat:@"http://sms.instatalkcommunications.com/apireq/GetRatingsForSMS?
   t=1&h=admin&cid=%@&token=1234567890&email=test@test.com",self.Id];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
   [request setRequestMethod:@"GET"];
    [request setTag:1];
   [request setDelegate:self];
   [request startAsynchronous];

     }

//这里有类似按钮

    - (IBAction)btnlike:(id)sender
    {
         NSLog(@"keypress");

         [self sendlike];
         [self getLike];

         UIButton *btnlike = (UIButton *) sender;

           btnlike.enabled = NO;
          UIButton *btndislike = (UIButton *) sender;

            btndislike.enabled = NO;

        }

//这里我是不喜欢按钮的行动

       - (IBAction)btndislike:(id)sender
        {
            NSLog(@"keypress");
           [self senddislike];
           [self getLike];
            UIButton *btndislike = (UIButton *) sender;
           btndislike.enabled = NO;
             _btnlike.enabled = NO;

        }



        @end

1 个答案:

答案 0 :(得分:0)

由于我不是母语为英语的人,我希望我已经正确地理解了你想要实现的目标。

如果我理解正确,你有一个'喜欢'和'不喜欢'按钮,当用户按下其中一个按钮时,你想要两个都被禁用, 哪种方式适用于当前运行。
但是,如果我理解正确,下次应用程序运行时,按钮会“重置”以启用,而您不希望这样,
您希望每次应用程序运行时都禁用该按钮。

如果我理解正确,“保存”对象状态的方法是使用NSUserDefaults

您可能有比下面示例更好的方法来实现它,但它应该可以解决问题,并且还可以让您更好地了解如何实现它,因为您需要修改我的代码。

添加以下方法:

-(void)disableButtons {  
    [[NSUserDefaults standardUserDefaults] setBool: YES forKey: @"buttonsDisabled"];  
    [[NSUserDefaults standardUserDefaults] synchronize];  
}  

现在,在“赞”和“不喜欢”按钮的末尾,只需通过添加代码调用以下方法:

[self disableButtons]  

现在,将以下内容添加到viewDidLoad

// someplace in viewDidLoad  
BOOL bottunsDisabled = [[NSUserDefaults standardUserDefaults] boolForKey: @"buttonsDisabled"];  
if(buttonsDisabled) {  
    self.btndislike.enabled = NO;  
    self.btnlike.enabled = NO;  
}  

按钮默认状态已启用,因此上述代码将更改它们,因为它们在之前的运行中已被禁用。
请注意,我还假设您有对按钮的引用(我假设您已在界面构建器中创建它们),如果没有,只需创建一个将保存对它们的引用的属性/ ivar。

正如上面提到的 Ian MacDonald ,在“赞”按钮方法中,您只禁用“赞”按钮两次,而不是禁用“不喜欢”按钮。
为了解决这个问题,请将代码更改为以下内容(同样,我假设您对这两个代码都有一些参考):

- (IBAction)btnlike:(id)sender  
{  
     NSLog(@"keypress");  

     [self sendlike];  
     [self getLike];  

     self.btnlike.enabled = NO;  
     self.btndislike.enabled = NO;  

     [self disableButtons];  
    }  

正如我上面所说,我不是母语为英语的人 所以我希望我的回答很清楚 如果事情不明确,请告诉我,我会尝试改写它。

祝你好运伴侣!