Buttons won't hide after returning from another View Controller - uibutton

I have an app with a tab bar and a number of tab bar items, each with its own VC. On the HOME tab bar view I have a set of UIButtons programmatically generated. I have one specific button ("Hide Buttons") that, when pressed, disables/hides the others. Works as expected.
However, when I tap a second tab bar item and then return to the original and tap the "Hide Buttons" button - they stay visible/enabled. If I set a breakpoint in the method called by the "Hide Buttons" button, I noted the memory address of one of the buttons and after tabbing to another view and returning, I notice the same UIButton memory address is different. Not sure why.
The UIButtons are all defined in the HOME VC inside the #implementation block in the form UIButton *originalButton; UIButton *hideButtonsButton; etc and initialized in viewDidAppear: using the following type of code:
// ORIGINAL BUTTON TAG = 1
originalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[originalButton addTarget:self action:#selector(originalButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
originalButton.frame = CGRectMake(20.0, 30.0, 100.0, 39.0);
UIImage *originalButtonImage = [[UIImage imageNamed:#"originalreg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *originalButtonImageHighlight = [[UIImage imageNamed:#"originalregblue.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[originalButton setBackgroundImage:originalButtonImage forState:UIControlStateNormal];
[originalButton setBackgroundImage:originalButtonImageHighlight forState:UIControlStateHighlighted];
[self.view addSubview:originalButton];
Any guidance appreciated!

Duh. I moved the call to the button initialization method to viewDidLoad vs. viewDidAppear. All is fine now.

Related

Common UIButton or UIbarbutton to navigationbar

How can we add common UIButton or UIBarButton or any UI object so that it appears in all navigationbars in the application.
The below code will work perfectly;
UIImage* image3 = [UIImage imageNamed:#"info.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width*4, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
//[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton setTitle:#"Category" forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(popUpPicker:)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
self.navigationItem.leftBarButtonItem = nil;
For these, you have to use category and then you have to call it in your every view controller to have the one code for every navigationabr button action.
There is no magic formula. One way or another, you have to add the same "UIButton or UIBarButton or any UI object" to the navigationItem of every UIViewController whose view will appear under this navigation controller's navigation bar.
You can reduce the amount of repeated code by creating this object in a single place, but the act of saying
self.navigationItem.rightBarButtonItem = thisItem
or whatever it is you want to do with the bar button item, will have to be performed separately and explicitly for every view controller.

Navigation bar button unable to go back to the previous view

I have followed the informations that i have found online and I have managed to show the button but when i clicked on it, nothing happens. I am new to iOS programming and I am not sure if I did anything wrong.
UIImage *backImage = [UIImage imageNamed:#"backIcon.PNG"];
// create the button and assign the image to the button
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = customBarButton;
and this code for the button
- (void)goBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES]; }
Thanks
First you don't have to hide the back button since you are adding your own, second try to replace your button target with the below:
[backButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
Edit:
The right way to use navigationController in storyboard will be as following:
1- Add a UInavigationController.
2- Add rootViewController relation to first VC1.
3- Add push segue from VC1 to VC2.
By this you can push and pop your VC2 properly.
Please consider to read this tutorial for better understanding storyboard, segues and navigation.

How to implement a overlay button which is floating over the content view

in some iPhone apps i saw a button which was floating over the content view, eg. in the app EyeEm. When the user is scrolling the content, the button remains where it is and is still an interaction element.
Ho do I implement this?
My approach would be:
Create a view with content
Put a button on it
But how to make the button floating?
edit:
The floating seems to be the default behavior. Interestingly addSubview and insertSubview have the same behavior when placing the button... both are floating over the content.
- (void)addOverlayButton {
UIButton *oButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[oButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[oButton setTitle:#"Show View" forState:UIControlStateNormal];
oButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:oButton];
//[self.view insertSubview:oButton aboveSubview:_scrollView]; // same result as addSubview.
// Both solutions let the button float over the content.
}
Add a subview to your window's content view with insertSubview:aboveSubview: method with your button and scrollview as arguments, but be careful: if two sibling views both have transparency, the resulting drawing behaviour is undefined.
if you using storyboard just add button over it, like
if you want to do with code insertSubview:buttonView aboveSubview:scrollView
no matters its table or scroll view

Custom UIButton background image not appearing until tapped

In app my, if you tap on a certain area a UIPopoverController appears with UIButtons that perform certain tasks when clicked. The UIButtons (called CableDisconnectButton) are a subclassed UIButton so I could add two additional properties to them. I also add UILabels to go over the buttons
However, the background images of the buttons are invisible or don't appear until I tap on the screen somewhere. The UIlabels show up fine, but not the buttons. It can be a tap on the UIPopoverController or anywhere else on the screen. Once I've tapped that first time, the buttons will be there until the app is closed. So, this only happens right after launch and up until I first open that UIPopover. I tap plenty of times before opening the popover.
The functionality of the buttons and everything else works fine, but the background images are hidden on that first launch and I have no idea why.
Here's how I create the buttons and UILabel:
//create custom button
CableDisconnectButton *removeConnectionButton = [CableDisconnectButton buttonWithType:UIButtonTypeCustom];
removeConnectionButton.frame = CGRectMake(x, y, 190, 80);
removeConnectionButton.backgroundColor = [UIColor clearColor];
[removeConnectionButton setBackgroundImage:[UIImage imageNamed:#"images/cable_disconnect_button.png"] forState:UIControlStateNormal];
[removeConnectionButton setBackgroundImage:[UIImage imageNamed:#"images/cable_disconnect_button_over.png"] forState:UIControlStateHighlighted];
//set input and output jacks to button properties
removeConnectionButton.inputJack = inputJack;
removeConnectionButton.outputJack = self.outputJackView;
//add action to button
[removeConnectionButton addTarget:self action:#selector(removeConnectionButtonTarget:) forControlEvents:UIControlEventTouchUpInside];
//create label for output
UILabel *outputConnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(x+18, y+5, 180, 22)];
outputConnectionLabel.backgroundColor = [UIColor clearColor];
outputConnectionLabel.textColor = [UIColor whiteColor];
outputConnectionLabel.text = self.outputJackView.jackDisplayName;
outputConnectionLabel.font = [UIFont systemFontOfSize:16];
//add subviews
[self addSubview:removeConnectionButton];
[self addSubview:outputConnectionLabel];
I've tried to add a regular, non-custom UIButton and it appears without the tap. I suspect it may have something to do with the subclassed UIButton, but I'm not sure why. The extra properties added to the UIButton are strings that are crucial to the functionality of the and can't be omitted.
After beating my head off the desk for days, I ran into the "Clean Build Folder" option. I've cleaned the project plenty of times, but wasn't aware of "Clean Build Folder". To execute this, simple hold the Option key, click Product from the menu and select Clean Build Folder.
So, if your app isn't behaving the way it should and it makes no sense AT ALL, try this.

UIButton not clickable after UITableView scrolled

I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.