tvOS: Setting focus order - objective-c

I currently have 3 objects called MenuButton that's a child of UIButton. These are defined in a row as shown in the image.
I've defined the highlight states in my MenuButton -didUpdateFocusInContext:withAnimationCoordinator method:
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context
withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
if (context.nextFocusedView == self)
{
[self setTitleColor:[UIColor whiteColor]
forState:UIControlStateNormal];
}
else if (context.previouslyFocusedView == self)
{
[self setTitleColor:[UIColor colorWithRed:25.0/255.0
green:23.0/255.0
blue:16.0/255.0
alpha:1.0]
forState:UIControlStateNormal];
}
}
I've also set canBecomeFocused in the MenuButton object via:
- (BOOL)canBecomeFocused
{
return YES;
}
I've set my initial focus button using:
[_camerasButton setNeedsFocusUpdate]
When I swipe right on the Simulator remote, I'm not able to get it to focus on Deliveries.
Can someone please explain how I can use UIFocusGuide to implement this correctly in Objective-C?
UPDATE 1
Based on JustinVoss' suggestion on the first comment, I set breakpoint and did a _whyIsThisViewNotFocusable on the debug console. Received this hint:
(lldb) po [(UIView*)0x7ff68054f7c0 _whyIsThisViewNotFocusable]
ISSUE: This view may not be focusable because other views or focus guides are occluding it.
I'm defining my MenuButton frames as shown:
MenuButton *deliveriesButton = [MenuButton buttonWithType:UIButtonTypeCustom];
[deliveriesButton setTitle:#"Deliveries" forState:UIControlStateNormal];
[deliveriesButton setTitleColor:[UIColor colorWithRed:25.0/255.0
green:23.0/255.0
blue:16.0/255.0
alpha:1.0]
forState:UIControlStateNormal];
[deliveriesButton.titleLabel setFont:[Constants lightFontOfSize:39]];
[deliveriesButton sizeToFit];
deliveriesButton.center = CGPointMake(viewWidth/2,
menuBarHeight / 2);
[self.view addSubview:deliveriesButton];
_deliveriesButton = deliveriesButton;

Using the solution referenced by Justin Voss in the comments above, I went through the UIViews in my UIViewController and set:
view.userInteractionEnabled = NO;
on any view that I did not need to have a selection on (including background UIImageView objects). This resolved the problem.
Hope this helps someone else

Related

Show touch when clicked UIButton Objective-C

I have a UIButton that has only an UIImage set, when Clicked (long press) the images changes automatically with a shadow, What I want is that even if the button gets clicked but not with a long press the images should have the shadow. I already tried from IB to set the UIButton properties Reverse on Highlight and Shows Touch On Highlight but with no results
This is about UIControl Class. It's default to be with shadow. So if you wanna customize it, you should addTarget to your button to every action on UIButton like tapping,long press,touchUpInside and I think it s not about long press it's just you can't realize the difference on tapping.
in one of my project I customize my button actions like this:
buyButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*3/4-2, 0, self.frame.size.width/4+4, self.frame.size.height)];
[buyButton addTarget:self action:#selector(buyButtonClicked:) forControlEvents:UIControlEventTouchDown];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpInside];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpOutside];
and methods of this like:
- (void) buyButtonClicked:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fede32"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fdca2e"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"cfd3dc"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"c0c5d0"];
}
}
- (void) buyButtonNormal:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fdca2e"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fede32"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"c0c5d0"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"cfd3dc"];
}
[delegate purchaseOffer:selectedOffer];
}
If you need more information about UIButton actions there are a lot of question on this site.

one button in each section of uiTableView objective-C

I have a uitableview and in my table I have different section and in each section different rows,I want to add one button in last row of each sections
I used sectionFooter, and here is my code, but I don't know why I have it just in my last section of table and also it's not appear with scroll.
Here is my code:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
footerView = [[UIView alloc] init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// [button setBackgroundImage:image forState:UIControlStateNormal];
//the button should be as big as a table view cell
[button setFrame:CGRectMake(10, 3, 300, 44)];
//set title, font size and font color
[button setTitle:#"Report New Time" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(removeAction:)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
}
//return the view for the footer
return footerView;
}
what is wrong here, Would you please help me!
Thanks in advance!
You need to remove the if statement in your tableView:viewForFooterInSection: method. That's causing only one view to be created.
One UIView has to have only one superview. You have to create footerView for every section.
You are reusing the footerView for every section, partly by just keeping one footerView instance variable, partly by just creating it once. Every time UITableView asks you for a footer view for a section and it gets back the same view, that view is moved since it can't stay in all those places at the same time. The solution is to not use a single view.
Use an NSMutableDictionary to hold NSNumbers with the section number as key and the appropriate footer view as the value. Change the code to work with the section's "slot" in the dictionary instead of a single instance variable.

How to have a UIKit button open another view?

I have a grid of buttons in one page. I want the buttons to lead to another view when clicked. What should I add to the following action to change the view?
-(void)buttonPressed:(UIButton *)button {
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
}
The grid of buttons is created with:
{
int rows = 13, columns = 4;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 80*columns, 32*rows)];
int currentTag = 0;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
// [button.layer setBorderWidth:1.0];
// [button.layer setBorderColor:UIColor blackColor]];
button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
button.tag = currentTag;
currentTag++;
[button.layer setBorderColor: [[UIColor blackColor] CGColor]];
[button.layer setBorderWidth: 1.0];
[button setTitle:[NSString stringWithFormat:#"%d",currentTag] forState:UIControlStateNormal];
button.frame = CGRectMake(80*x, 32*y, 80, 32);
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
}
}
// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];
}
Maybe you want to connect an action?
[button addTarget:self
selector:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
(check syntax, this is by memory...)
Rinju's answer would work, but it needs a little more explanation. I'm not sure why exactly you would like to create this grid of buttons (more info would be nice), but if you want each button to show a new view, then tags are important!
If you want to open the same view, but with different properties shown on that view, you'd need to create a new view controller class. Let's call this DetailViewController.
Now, in your buttonPressed: method, you're going to need to instantiate this view controller and set a property of it!
#interface DetailViewController : UIViewController
#property (nonatomic, assign) int buttonTag;
...
//other methods, properties, etc. here
...
#end
Now, in your view controller where the buttons are, you can do this (for the sake of simplicity, I will assume you are using storyboards. If not, it can still be easily accomplished similar to what Rinju has done).
-(void)buttonPressed:(UIButton*)button {
[self performSegueWithIdentifier:#"DetailView" sender:button];
}
Now, you can implement the prepareForSegue method, which is called just before the segue fires:
-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {
if( [[segue identifier] isEqualToString:#"DetailView"] ) {
DetailViewController *detailView = [segue destinationViewController];
detailView.buttonTag = ((UIButton*)sender).tag;
}
}
Now that the tag is set for your new detail view controller, you can probably use switch statements (or better yet, a model object which is passed this buttonTag) in viewDidLoad: of the DetailViewController to set up the UI there. Basically what we are doing is using the tag of the button to distinguish which button was pressed, and based on this create a new view!
What you may want to do is create a typedef enum structure to make names for the tags instead of using raw integers. This will increase the readability of your code, and also will help you in not getting confused ;)
-(void)buttonPressed:(UIButton *)button
{
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
Cart *crtObj=[[Cart alloc]initWithNibName:#"Cart" bundle:nil];
[self.navigationController pushViewController:crtObj animated:YES];
[crtObj release];
}

How to change the image of the button on click of the button in objective c

I have placed a pause button in UI, where when the user clicks the button, the image of button from pause image it has to change to the start image. I have placed both the start and the pause images in the bundle, I am able to do this,but when i click on start again , it should show pause image button,How to do this, the following is the code i am using,
-(IBAction)btnClked:(id)sender
{
[pauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
You need a variable to keep track of that state change. A BOOL is fine.
But using your code, you can "abuse" the tag property of UIButton to tell if it's playing (tag == 1) or paused (tag == 0)
-(IBAction)btnClked:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.tag == 1) {
btn.tag = 0;
[btn setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
} else {
btn.tag = 1;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
}
You can use UIButton's selected state to do the same. For example, while initiating the button you have to define 2 images. One for normal state and other for selected state. When touch event of the button fired, you have to make it selected. If it is already selected you need to deselect it. This is much better, than maintaining additional variables to check selected state of the button. Also you could improve the performance with this approach.
Code example
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[startButton setBackgroundImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[startButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
}
- (IBAction)modeButtonClick:(id)sender {
UIButton *startButton = (UIButton *)sender;
[startButton setSelected:![startButton isSelected]];
}
If you are creating the button from story board or XIBs set the backgroud image for default state config and selected state config.

How can I create a ToggleButton in Obj-C? Skin the UISwitch or subclass the UIButton? [duplicate]

Is there either a way to implement UISwitch with custom graphics for the switch-states?
Or as an alternative the other way round, an UIButton with UISwitch functionality?
UIButton already supports a "switch" functionality.
Just set a different image in Interface Builder for the "Selected State Configuration", and use the selected property of UIButton to toggle its state.
set the image to show on selected state:
[button setImage:[UIImage imageNamed:#"btn_graphics"] forState:UIControlStateSelected];
and then on touch up inside selector, set:
button.selected = YES;
if you want this to cancel another button's selection, set:
otherButton.selected = NO;
To build on what PGB and nurne said above, after you set your states and attach a selector (event method) you would want to put this code in that selector.
- (IBAction)cost:(id)sender
{
//Toggle current state and save
self.buttonTest.selected = !self.buttonTest.selected;
/**
The rest of your method goes here.
*/
}
For programmatically inclined:
-(void) addToggleButton {
CGRect aframe = CGRectMake(0,0,100,100);
UIImage *selectedImage = [UIImage imageNamed:#"selected"];
UIImage *unselectedImage = [UIImage imageNamed:#"unselected"];
self.toggleUIButton = [[UIButton alloc] initWithFrame:aframe];
[self.toggleUIButton setImage:unselectedImage forState:UIControlStateNormal];
[self.toggleUIButton setImage:selectedImage forState:UIControlStateSelected];
[self.toggleUIButton addTarget:self
action:#selector(clickToggle:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.toggleUIButton];
}
-(void) clickToggle:(id) sender {
BOOL isSelected = [(UIButton *)sender isSelected];
[(UIButton *) sender setSelected:!isSelected];
}