Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果按下后退按钮,则显示所选按钮_Ios_Objective C_Uibutton - Fatal编程技术网

Ios 如果按下后退按钮,则显示所选按钮

Ios 如果按下后退按钮,则显示所选按钮,ios,objective-c,uibutton,Ios,Objective C,Uibutton,在第一页我有一些按钮,如果我按下任何按钮,它将移动到第二页,在这里,如果我按下导航栏上的后退按钮,它将移动到第一页 我已经根据数组计数动态创建了按钮,我想在返回到第一页时显示所选的按钮,如何才能做到这一点?请帮助我做到这一点。以下是我用于创建按钮的代码 int buttonheight = 30; int horizontalPadding = 20; int verticalPadding = 20; int totalwidth = self.view.frame.size.width;

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

我已经根据数组计数动态创建了按钮,我想在返回到第一页时显示所选的按钮,如何才能做到这一点?请帮助我做到这一点。以下是我用于创建按钮的代码

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];    
}
int buttonheight=30;
int水平填充=20;
int垂直填充=20;
int totalwidth=self.view.frame.size.width;
int x=10;
int y=150;
对于(int i=0;i总宽度){
y=y+按钮高度+垂直填充;
x=10;
}
UIButton*word=[UIButton按钮类型:UIButtonyPeroundRect];
self.word=单词;
NSLog(@“%@”,NSStringFromCGRect(帧));
word=[UIButton按钮类型:UIButtonTypeRondRect];
word.frame=CGRectMake(x,y,CGRectGetWidth(frame)+5,CGRectGetHeight(frame));
[字集标题:状态的滴定度:uicontrol状态正常];
word.backgroundColor=[UIColor colorWithRed:30.0/255.0绿色:134.0/255.0蓝色:255.0/255.0 alpha:1.0];
[word setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[word setTag:i];
[word addTarget:self action:@selector(BTN1点击:)for ControlEvents:UIControlEventTouchUpInside];
word.layer.borderColor=[UIColor blackColor].CGColor;
word.layer.borderWidth=1.0f;
word.layer.cornerRadius=5;
[self.view addSubview:word];
x=x+水平填充+CGRectGetWidth(帧);
}
-(iAction)BTN11:(id)发送方{
NSString*btnTitle;
btnTitle=[(UIButton*)发送方当前标题];
NSLog(@“%@”,自我名称);
ReasonViewController*ReasonVC=[self.storyboard实例化eViewController标识符:@“ReasonVC”];
ReasonVC.btnTitle=btnTitle;
NSLog(@“%@”,self.imageArray);
ReasonVC.imageArray1=self.imageArray;
[self.navigationController pushViewController:ReasonVC:YES];
}

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 
}

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

您必须将按钮标记存储在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];    
    }
我得到下面的截图,如果我点击任何一个按钮,它将移动到下一页,背景颜色变为红色,然后通过点击返回回到同一页,现在我点击了另一个按钮,它也显示为红色。 我在按钮操作方法中使用了这两行:

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

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

@property (assign, nonatomic) long int btnTag;
=======类别视图控制器.h============

import "AppDelegate.h"

@interface CategoryViewController : UIViewController
{
    AppDelegate *appdel;
}
=============类别视图控制器.m======= 添加代码

以下
[word setTag:i]代码

然后加上

 UIButton *btn = (UIButton *) sender;
 appdel.btnTag = btn.tag;

-(iAction)btn1Tapped:(id)sender
方法中

返回时是否重新创建?为什么取消选择它们?请添加您的按钮单击方法,即您的BTN11tapped:@TLuck我已使用按钮单击更新了我的代码method@user6183984请查看我的代码您想仅为1或为所有已选择的按钮选择?是,您可以为按钮设置选中和取消选中的图像,并根据点击的位置更改状态。谢谢@TLuck,我会尝试让您知道显示错误的信息,如使用未声明的标识符appdel。您是否添加了@property(assign,nonatomic)int btnTag;在appdelegate.h@user6183984上,是的。我为BTNTA添加了属性,那么AppDelegate*appdel=(AppDelegate*)[[UIApplication sharedApplication]delegate]呢;你添加了吗?你的问题@user6183984解决了吗
 UIButton *btn = (UIButton *) sender;
 appdel.btnTag = btn.tag;