如果按下后退按钮,则显示选定的按钮

时间:2016-05-12 06:14:59

标签: ios objective-c uibutton

在第一页我有一些按钮,如果我按任何按钮它将移动到第二页,如果我按下导航栏中的后退按钮它将移动到第一页

我已根据数组计数动态创建按钮,我想在移回第一页时显示所选按钮,我该怎么做?请帮我这样做。我有的代码是用于创建按钮

int buttonheight = 30;
int horizontalPadding = 20;
int verticalPadding = 20;

int totalwidth = self.view.frame.size.width;

int x = 10;
int y = 150;

for (int i=0; i<array.count; i++)
{

    NSString* titre = [array objectAtIndex:i];

    //
    CGSize contstrainedSize = CGSizeMake(200, 40);//The maximum width and height

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont systemFontOfSize:20.0], NSFontAttributeName,
                                          nil];
    CGRect frame = [titre boundingRectWithSize:contstrainedSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
    int xpos = x + CGRectGetWidth(frame);

    if (xpos > totalwidth) {

        y =y +buttonheight+ verticalPadding;
        x = 10;


    }


    UIButton * word= [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.word = word;

    NSLog(@"%@", NSStringFromCGRect(frame));
    word = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    word.frame = CGRectMake(x, y, CGRectGetWidth(frame)+5, CGRectGetHeight(frame));
    [word setTitle:titre forState:UIControlStateNormal];

    word.backgroundColor = [UIColor colorWithRed:30.0/255.0 green:134.0/255.0 blue:255.0/255.0 alpha:1.0];

    [word setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


    [word setTag:i];
    [word addTarget:self action:@selector(btn1Tapped:) forControlEvents:UIControlEventTouchUpInside];
    word.layer.borderColor = [UIColor blackColor].CGColor;
    word.layer.borderWidth = 1.0f;
    word.layer.cornerRadius = 5;

    [self.view addSubview:word];

    x =x+horizontalPadding+CGRectGetWidth(frame);

}
- (IBAction)btn1Tapped:(id)sender {

NSString *btnTitle;
btnTitle = [(UIButton *)sender currentTitle];
NSLog(@"%@",self.title);
ReasonViewController *ReasonVC = [self.storyboard     instantiateViewControllerWithIdentifier:@"ReasonVC"];
ReasonVC.btnTitle = btnTitle;
NSLog(@"%@",self.imageArray);
ReasonVC.imageArray1 = self.imageArray;
[self.navigationController pushViewController:ReasonVC animated:YES];    
}

3 个答案:

答案 0 :(得分:1)

UIButton对象具有不同的状态,包括“选定状态”。您可以为所需的每个州设置不同的背景图像或标题。当你按它时,你可以改变它的状态:

//After you inited your UIButton Object do this:
[button setBackgroundImage:selectedBGImage forState:UIControlStateSelected];
[button setBackgroundImage:deselectedBGImage forState:UIControlStateNormal];
//You can also find the title change method for state accordingly


- (void)buttonAction:(UIButton *)sender
{ 
  sender.selected = !sender.selected;
//TODO 
}

因此,当您弹回时,该按钮将保持其“选中”状态。

答案 1 :(得分:1)

===== Appdelegate.h ======

@property (assign, nonatomic) long int btnTag;

======= CategoryViewController.h ============

import "AppDelegate.h"

@interface CategoryViewController : UIViewController
{
    AppDelegate *appdel;
}

=========== CategoryViewController.m ======= 添加代码

if(appdel.btnTag == word.tag)
{
   word.selected = YES;
}

低于[word setTag:i];代码

然后添加

 UIButton *btn = (UIButton *) sender;
 appdel.btnTag = btn.tag;
- (IBAction)btn1Tapped:(id)sender方法

中的

答案 2 :(得分:0)

您必须在appdelegate中存储按钮标记,同时使用按钮标记/ i值创建按钮检查并选择。请看下面的代码

在AppDelegate.h中

@property (assign, nonatomic) int btnTag;

在当前的ViewController上创建一个AppDelegate对象,然后

AppDelegate *appdel  = (AppDelegate *)[[UIApplication sharedApplication] delegate];
int buttonheight = 30;
.
.
.
    [word setTag:i];
    if(word.tag == appdel.btnTag)//appdel is object of AppDelegate
    {
         word.selected = YES;
    }
    [word addTarget:self action:@selector(btn1Tapped:) forControlEvents:UIControlEventTouchUpInside];
    word.layer.borderColor = [UIColor blackColor].CGColor;
    word.layer.borderWidth = 1.0f;
    word.layer.cornerRadius = 5;

    [self.view addSubview:word];

    x =x+horizontalPadding+CGRectGetWidth(frame);

}

    - (IBAction)btn1Tapped:(id)sender {
    UIButton *btn = (UIButton *) sender;
    appdel.btnTag = btn.tag;
    NSString *btnTitle;
    btnTitle = [(UIButton *)sender currentTitle];
    NSLog(@"%@",self.title);
    ReasonViewController *ReasonVC = [self.storyboard     instantiateViewControllerWithIdentifier:@"ReasonVC"];
    ReasonVC.btnTitle = btnTitle;
    NSLog(@"%@",self.imageArray);
    ReasonVC.imageArray1 = self.imageArray;
    [self.navigationController pushViewController:ReasonVC animated:YES];    
    }

enter image description here 我正如下面的截图,如果我点击任何按钮它将移动到下一页并且backgroundcolor变为红色,并通过单击返回返回到同一页面,现在我点击另一个按钮它也显示为红色。 我在按钮操作方法中使用了这两行:

UIButton *btn = (UIButton *) sender;
[btn setBackgroundColor:[UIColor redColor]];