I want to create following rating views shown below. How is it possible to do it
I have got 3 conditions.I have got label in top and below that are stars and at the end there is TextField. Good,Amazing,Horrific is set according to number of stars. When there is 1 Star i.e horrific condition we have to present four button Order issues, timing, friendliness,Cleanliness.And UITextField is shift below it
I have implemented following code
#pragma mark - Btn Action on start click
- (IBAction)Btn_FirstHorrific:(id)sender {
[self ReviewHorrific];
[self NumOfStarWorstCase];
str_deliveryBoyRating=#"1";
str_customerComment=#"Horrific";
}
- (IBAction)Btn_SecondNah:(id)sender {
[self ReviewSecondNah];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"2";
str_customerComment=#"nah!";
}
- (IBAction)Btn_ThirdItsOk:(id)sender {
[self ReviewItsOk];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"3";
str_customerComment=#"Its ok";
}
- (IBAction)Btn_FourthGood:(id)sender {
[self ReviewGood];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"4";
str_customerComment=#"good";
}
- (IBAction)Btn_FifthAmazing:(id)sender {
[self ReviewAmazing];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"5";
str_customerComment=#"Amazing";
}
#pragma mark - Change star colour in Number of star changes
- (void)ReviewHorrific{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = [UIColor whiteColor];
Btn_ThirdItsOk.tintColor = [UIColor whiteColor];
Btn_FourthGood.tintColor = [UIColor whiteColor];
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Please report your issue";
lbl_RateType.text=#"Horrific";
self.View_WorstCase.hidden = FALSE;
}
- (void)ReviewSecondNah{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = [UIColor whiteColor];
Btn_FourthGood.tintColor = [UIColor whiteColor];
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Please report your issue";
lbl_RateType.text=#"Nah!!";
}
- (void)ReviewItsOk{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
Btn_FourthGood.tintColor = [UIColor whiteColor];
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Please report your issue";
lbl_RateType.text=#"It's OK";
}
- (void)ReviewGood{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
Btn_FourthGood.tintColor = UIColorFromRGB(0xff8133);
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Heap praise if you would like";
lbl_RateType.text=#"Good";
}
- (void)ReviewAmazing{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
Btn_FourthGood.tintColor = UIColorFromRGB(0xff8133);
Btn_FifthAmazing.tintColor = UIColorFromRGB(0xff8133);
lbl_report.text=#"Glad to hear it. Any comments or feedback?";
lbl_RateType.text=#"Amazing";
}
Step 1)
Add 5 UIButton to your UI file. Then in your header (.h) code, declare the UIButton like so:
IBOutlet UIButton *starOne;
IBOutlet UIButton *starTwo;
IBOutlet UIButton *starThree;
IBOutlet UIButton *starFour;
IBOutlet UIButton *starFive;
Then back in your UI file, link the star button views to your code. Then add the default (non-selected) star images to your buttons (just drag and drop to do this).
Step 2)
Back in your header code, add 5 IBAction methods. These will detect when the star buttons are pressed:
-(IBAction)press_1;
-(IBAction)press_2;
-(IBAction)press_3;
-(IBAction)press_4;
-(IBAction)press_5;
Then add a method as well. This method will handle the star images.
-(void)set_stars:(int)num :(NSString *)desc
Then declare in integer in your header file. This will tell you what the rating is, like so:
int rating;
Also declare an array (this will contain pointers to your star buttons):
NSArray *_starButtons;
Lastly, declare a UILabel to show the name for each rating (and like it to your UI), like so:
IBOutlet UILabel *rating_name;
Step 3)
In your implementation (.m) code, add in the following:
-(IBAction)press_1 {
[self set_stars:1 :#"Horrific"];
}
-(IBAction)press_2 {
[self set_stars:2 :#"nah!"];
}
-(IBAction)press_3 {
[self set_stars:3 :#"Its ok"];
}
-(IBAction)press_4 {
[self set_stars:4 :#"good"];
}
-(IBAction)press_5 {
[self set_stars:5 :#"Amazing"];
}
-(void)set_stars:(int)num :(NSString *)desc {
rating_name.text = [NSString stringWithFormat:#"%#", desc];
for (int loop = 0; loop < 5; loop++) {
if ((loop + 1) <= num) {
[((UIButton*)_starButtons[loop]) setBackgroundImage:[UIImage imageNamed:#"star_ON.png"] forState:UIControlStateNormal];
}
else {
[((UIButton*)_starButtons[loop]) setBackgroundImage:[UIImage imageNamed:#"star_OFF.png"] forState:UIControlStateNormal];
}
}
rating = num;
}
Finally, dont forget to populate the array with your buttons in the viewDidLoad method like so:
_starButtons = #[starOne, starTwo, starThree, starFour, starFive];
Related
I'd like to know when BarButton items gets the accessibility focus or loses the accessibility focus and so I implemented the informal protocol methods of UIAccessibilityFocus but it's not still firing.
extension UIBarButtonItem {
override open func accessibilityElementDidBecomeFocused() {
if self.accessibilityElementIsFocused() {
print("My element has become focused.")
}
}
override open func accessibilityElementDidLoseFocus() {
if self.accessibilityElementIsFocused() {
print("My element has lost focus.")
}
}
override open func accessibilityElementIsFocused() -> Bool {
if (self.accessibilityIdentifier == "hamburger") {
return true
} else {
return false
}
}
I imported the swift file into viewcontroller as well
#import "Sample-Swift.h"
then I tried subclassing and implemented the methods that also didn't work
.h header file
#import <UIKit/UIKit.h>
#import <UIKit/UIAccessibility.h>
NS_ASSUME_NONNULL_BEGIN
#interface HamburgerButton : UIBarButtonItem
#end
NS_ASSUME_NONNULL_END
.m implementation file
#implementation HamburgerButton
- (BOOL)isAccessibilityElement
{
return YES;
}
- (void)accessibilityElementDidBecomeFocused {
if ([self accessibilityElementIsFocused]) {
NSLog(#"My element has become focused.");
}
}
- (void)accessibilityElementDidLoseFocus {
if ([self accessibilityElementIsFocused]) {
NSLog(#"My element has lost focus.");
}
}
- (BOOL)accessibilityElementIsFocused {
if ([self.accessibilityIdentifier isEqualToString:#"hamburger"]) {
return YES;
} else {
return NO;
}
}
#end
Here is the implementation in view controller
HamburgerButton *leftButton = [[HamburgerButton alloc]
initWithTitle:#"Hamburger"
style:UIBarButtonItemStylePlain
target:self
action:#selector(flipView:)];
leftButton.accessibilityIdentifier=#"Hamburger";
leftButton.tag = 88;
leftButton.isAccessibilityElement = YES;
HamburgerButton *rightButton = [[HamburgerButton alloc]
initWithTitle:#"Chat"
style:UIBarButtonItemStylePlain
target:self
action:#selector(flipView:)];
rightButton.accessibilityIdentifier=#"Chat";
rightButton.tag = 89;
rightButton.isAccessibilityElement = YES;
self.navigationItem.leftBarButtonItem = leftButton;
self.navigationItem.rightBarButtonItem = rightButton;
Even though the focus comes and goes away from bar button , I'm not getting the call back on accessibilityElementDidBecomeFocused
Any ideas what could be done to get accessibilityElementDidBecomeFocused firing ?
Update 1:
I could achieve this functionality by the means of notification observer but it's not giving enough information about the receiver of focus so couldn't differentiate one bar button from the other.
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(hamburgerGotFocus:) name:UIAccessibilityElementFocusedNotification object:nil];
and find the selector method below
-(void)hamburgerGotFocus:(NSNotification *) notification{
NSLog(#"Focus:%#",notification);
UIView *receiver = notification.userInfo[#"UIAccessibilityFocusedElementKey"];
if(receiver!=nil){
NSString *strElement = [[NSString alloc]initWithFormat:#"%#",notification.userInfo[#"UIAccessibilityFocusedElementKey"]];
if([strElement containsString:#"UIButtonBarButton"]){
}
}
}
Here is the log of notification
2022-10-12 18:57:03.992859+0530 Sample[32427:1579550] Focus:NSConcreteNotification 0x280ac9980 {name = UIAccessibilityElementFocusedNotification; userInfo = {
UIAccessibilityAssistiveTechnologyKey = UIAccessibilityNotificationVoiceOverIdentifier;
UIAccessibilityFocusedElementKey = "<_UIButtonBarButton: 0x10690fce0; frame = (0 0; 49 44); tintColor = UIExtendedSRGBColorSpace 0 1 0 1; gestureRecognizers = <NSArray: 0x2804262e0>; layer = <CALayer: 0x280afa9e0>>";
}}
Update 2:
I tried doing this with UIlabel using Category and subclassing both worked
#interface SampleLabel : UILabel
#end
#implementation SampleLabel
- (void)accessibilityElementDidBecomeFocused {
NSLog(#"accessibilityIdentifier:%#",self.accessibilityIdentifier);
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, #"sample label from subclass");
}
- (void)accessibilityElementDidLoseFocus {
if ([self accessibilityElementIsFocused]) {
NSLog(#"My element has lost focus.subclass");
}
}
- (BOOL)accessibilityElementIsFocused {
return YES;
}
#end
By means of category
#interface UILabel (SampleLabel1)
#end
#implementation UILabel (SampleLabel1)
- (void)accessibilityElementDidBecomeFocused {
NSLog(#"accessibilityIdentifier:%#",self.accessibilityIdentifier);
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, #"sample label from category");
}
- (void)accessibilityElementDidLoseFocus {
if ([self accessibilityElementIsFocused]) {
NSLog(#"My element has lost focus.Category");
}
}
- (BOOL)accessibilityElementIsFocused {
return YES;
}
#end
I'm wondering whether accessibilityElementDidBecomeFocused is not compatible with UIBarButtonItem ?
FYI:
I'm following this tutorial to implement accessibilityElementDidLoseFocus.
accessibilityElementDidBecomeFocused
is not firing for bar button but it's firing for UIButton. So I just created a bar button using UIButton like below and the problem is fixed
// Image
UIImage *img1 = [UIImage imageNamed:#"img1"];
// Button
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:img1 forState:UIControlStateNormal];
btn1.frame = CGRectMake(0.0, 0.0, img1.size.width, img1.size.height);
btn1.tintColor = [UIColor greenColor];
[btn1 addTarget:self action:#selector(barButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
// Bar button
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc]initWithCustomView:btn1];
// Accessibility element
btn1.isAccessibilityElement = true;
img1.isAccessibilityElement = false;
btn1.accessibilityLabel = #"some accessibility text";
btn1.accessibilityTraits = UIAccessibilityTraitNone;
self.navigationItem.rightBarButtonItem = barButton1;
Hi I am new to iOS development..Can any one help me..
I have added multiple UILabel inside UIScrollview based on array count..For example if array count is 3 means ..then In scrollview adding 3 view along with UILabel in each view..
So now 3 view having 3 different UILabels..
But now I want to change colour of UILabel text in different views based on requirement..but I am not able to update colour…
Its change colour of UILabel text only for last index ..
I have written code in ScrollViewDidScroll:(UIScrollView *)scrollView
Any suggestion ..
self.robotScrollView.contentSize = CGSizeMake(robotCounts*Robot_ScrollView_Width, kRobotSrollViewH);
for (int i=0; i<robotCounts; i++) {
self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
self.robotLabel.textAlignment = NSTextAlignmentCenter;
self.robotLabel.backgroundColor = [UIColor clearColor];
self.robotLabel.textColor = [UIColor blackColor];
[self.robotScrollView addSubview:self.robotLabel];
if (kRemoteManager.robotsArray.count == 0) {
self.robotLabel.text = #"";
break;
}
DeviceBase *robot = kRemoteManager.robotsArray[i];
self.robotLabel.text = [NSString stringWithFormat:#"%#",robot.dName];
}
self.robotScrollView.contentOffset = CGPointMake(Robot_ScrollView_Width*currentRobotIndex, 0);
Changing UIlabeltext color in below scrollviewdidscroll method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.scrollView)
{
self.pageControl.currentPage = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
kRemoteManager.currentIndex = self.pageControl.currentPage;
[[NSNotificationCenter defaultCenter] postNotificationName:kNotify_Steward_ReloadData object:nil];
NSMutableArray *viewRemoteIDarray = [NSMutableArray array];
CardRemoteModel *model = kRemoteManager.cardRemoteArray[self.pageControl.currentPage];
if (![viewRemoteIDarray containsObject:#(model.remoteID)])
{
if (model.remoteType == kooKong_remoteType_AirCleaner || model.remoteType == kooKong_remoteType_AC)
{
self.robotLabel.textColor = [UIColor whiteColor];
}
else
{
self.robotLabel.textColor = [UIColor blackColor];
}
}
} else if (scrollView == self.robotScrollView) {
kRemoteManager.currentRobotIndex = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
self.leftBtn.hidden = NO;
self.rightBtn.hidden = NO;
if (kRemoteManager.currentRobotIndex == kRemoteManager.robotsArray.count-1) {
self.rightBtn.hidden = YES;
}
if (kRemoteManager.currentRobotIndex == 0){
self.leftBtn.hidden = YES;
}
}
}
There are three labels, but only one label property. The last one assigned is the only one you'll have access to later. One solution would be to keep an array of labels in the view controller.
#property(nonatomic, strong) NSMutableArray *labels;
In the posted method...
self.labels = [NSMutableArray array];
for (int i=0; i<robotCounts; i++) {
UILabel *robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
[self.labels addObject:robotLabel]; // <--- new
robotLabel.textAlignment = NSTextAlignmentCenter;
robotLabel.backgroundColor = [UIColor clearColor];
robotLabel.textColor = [UIColor blackColor];
[self.robotScrollView addSubview:robotLabel];
if (kRemoteManager.robotsArray.count == 0) {
robotLabel.text = #"当前无酷控机器人";
break;
}
DeviceBase *robot = kRemoteManager.robotsArray[i];
robotLabel.text = [NSString stringWithFormat:#"%#",robot.dName];
}
To change all of the colors:
- (void)setLabelColors:(UIColor *)color {
for (UILabel *label in self.labels) {
label.textColor = color;
}
}
Another idea would be to give each label a tag, and find them when you need them.
for (int i=0; i<robotCounts; i++) {
self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
self.robotLabel.tag = i+1;
// the remainder of this loop as you have it
To change all the colors...
- (void)setLabelColors:(UIColor *)color {
for (int i=0; i<robotCounts; i++) {
UILabel *label = (UILabel *)[self.robotScrollView viewWithTag:i+1];
label.textColor = color;
}
}
I have a problem with my App. I have a View with 25 Buttons with changed cornerRadius. Since iOS 10 it takes about 5-6 seconds till this View loads.
Here is the relevant code:
- (void)setupTiles {
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
}
[self colorTilesWithArray:currentTileColors];
}
When i remove the following lines, is loads the view instantly:
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
The Buttons are grouped in an outlet collection, in case that's necessary to know.
Does anybody have a solution?
Thanks in advance!
Nico
Image 1: This takes about 5-6 seconds
Image 2: This is the View that will be loaded
TileButton.h-File:
#interface TileButton : UIButton
#property (nonatomic) int colorMode;
+ (UIColor *)blue;
+ (UIColor *)red;
+ (UIColor *)green;
+ (UIColor *)yellow;
+(UIColor *)colorForColorCode:(int)colorCode;
-(int)colorMode;
#end
TileButton.m-File:
#implementation TileButton
- (instancetype)init {
self = [super init];
if (self) {
self.layer.cornerRadius = self.frame.size.width*0.3;
}
return self;
}
+(UIColor *)blue {
return [UIColor colorWithRed:41.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1];
}
+(UIColor *)red {
return [UIColor colorWithRed:255.0/255.0 green:71.0/255.0 blue:109.0/255.0 alpha:1];
}
+(UIColor *)green {
return [UIColor colorWithRed:0.0/255.0 green:185.0/255.0 blue:30.0/255.0 alpha:1];
}
+(UIColor *)yellow {
return [UIColor colorWithRed:255.0/255.0 green:198.0/255.0 blue:26.0/255.0 alpha:1];
}
+(UIColor *)colorForColorCode:(int)colorCode {
switch (colorCode) {
case 1:
return [TileButton blue];
break;
case 2:
return [TileButton red];
break;
case 3:
return [TileButton green];
break;
case 4:
return [TileButton yellow];
break;
default:
return [UIColor blackColor];
break;
}
}
-(int)colorMode {
if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton blue].CGColor))
return 1;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton red].CGColor))
return 2;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton green].CGColor))
return 3;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton yellow].CGColor))
return 4;
else
return -1;
}
#end
Since you are creating the buttons in Interface Builder you will need to set the cornerRadius in the awakeFromNib method. It should look something like this:
#implementation TileButton
-(void) awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = self.frame.size.width*0.3;
}
https://developer.apple.com/reference/objectivec/nsobject/1402907-awakefromnib
Why can't you set the corner radius outside the for loop?
UIButton *btn = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; // set your frame here
bin.layer.cornerRadius = btn.frame.size.width*0.3; // width stays the same, set it here
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
// you need to set the button frame here
btn.frame = CGRectMake(0,0,0,0);
[btn layoutIfNeeded];
}
[self colorTilesWithArray:currentTileColors];
Assuming that TileButton is a subclass of UIButton, on initialization, you can set the TileButton to have your custom color, width, and corner radius. That way all of your buttons already have the specified customization on button creation.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
On first Page i am having some buttons , if i press any button it will move to Second page , here if i press back button in the navigation bar it will move to first page
i have created the buttons dynamically based on the array count ,i want to show the selected button when i move back to first page , how can i do this ?Please help me to do this .here is the code that i have used for creating buttons
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];
}
=====Appdelegate.h========
#property (assign, nonatomic) long int btnTag;
=======CategoryViewController.h============
import "AppDelegate.h"
#interface CategoryViewController : UIViewController
{
AppDelegate *appdel;
}
===========CategoryViewController.m=======
add code
if(appdel.btnTag == word.tag)
{
word.selected = YES;
}
below [word setTag:i]; code
then add
UIButton *btn = (UIButton *) sender;
appdel.btnTag = btn.tag;
in - (IBAction)btn1Tapped:(id)sender method
The UIButton object has different states included the "Selected state". You can set different background image or title for each state you want. When you press it you can change its state like this:
//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
}
So, when you pop back, the button will keep its "selected" state.
You have to store button tag in appdelegate and while creating button check with button tag/i values and do selected. please see below code
in AppDelegate.h
#property (assign, nonatomic) int btnTag;
create an object of AppDelegate on your current ViewController then
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
I am getting like below screenshot , if i tapped any button it will move to next page and the backgroundcolor is changed to red color,and come back to same page by clicking back and now i tapped another button it is also showing in red color.
I have use this two lines in button action method :
UIButton *btn = (UIButton *) sender;
[btn setBackgroundColor:[UIColor redColor]];
I am almost new to create a custom cell. I have a custom cell and created 6 labels on each row with a custom button(btnEdit). The custom button is droped from .xib. The btnEdit create a frame on two label and if that frame is clicked it call another function which is working fine.
The only issue I have is if I click on one of the btnEdit in the row I don't want it to be clicked in the other row unless it is removed OR if one is selected and the other clicked it remove the first and frame the other.
Here is my code that I hope it helps;
.H
#interface PositionTableCell : UITableViewCell {
IBOutlet UILabel *lblSymbol;
IBOutlet UILabel *lblSpl;
IBOutlet UILabel *lblLpl;
IBOutlet UILabel *lblrate;
IBOutlet UILabel *lblAmount;
IBOutlet UILabel *lblO;
}
#property (nonatomic, assign) BOOL isSelected;
#property (nonatomic, assign) BOOL isRowSelected;
#end
.M
- (IBAction)btnEdit:(id)sender
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(value1)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[lblLpl addGestureRecognizer:tapGestureRecognizer];
UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(value2)];
tapGestureRecognizer2.numberOfTapsRequired = 1;
[lblSpl addGestureRecognizer:tapGestureRecognizer2];
if (!isSelected){
lblLpl.layer.borderColor = lblSpl.layer.borderColor = [UIColor blueColor].CGColor;
lblLpl.layer.borderWidth = lblSpl.layer.borderWidth = 5.0;
lblLpl.userInteractionEnabled = lblSpl.userInteractionEnabled= YES;
isRowSelected = YES;
isSelected = YES;
}
else if (isSelected){
lblLpl.layer.borderColor = lblSpl.layer.borderColor = [UIColor clearColor].CGColor;
lblLpl.layer.borderWidth = lblSpl.layer.borderWidth = 0;
lblLpl.userInteractionEnabled = lblSpl.userInteractionEnabled= NO;
isRowSelected = NO;
isSelected = NO;
}
[tapGestureRecognizer release];
[tapGestureRecognizer2 release];
NSLog(#"CLICKED");
}
It can be solved by using keeping a reference of your previously selected cell.
Declare a variable in your .m file, like:
static PositionTableCell *previousCell = nil;
And modify your method like:
- (IBAction)btnEdit:(id)sender
{
if (previousCell != nil && previousCell != self)
{
for (id subLabel in [[previousCell contentView] subviews])
{
if ([subLabel isKindOfClass:[UILabel class]])
{
UILabel *tempLabel = (UILabel *)subLabel;
tempLabel.layer.borderColor = [UIColor clearColor].CGColor;
tempLabel.layer.borderWidth = 0;
tempLabel.userInteractionEnabled = NO;
isRowSelected = NO;
isSelected = NO;
}
}
}
// Other codes here
}