Can't get UILabel to display from boolean condition - ios

I can't for the life of me figure out why this won't work. I'm trying to display a label after determining a boolean condition. No matter what I do, the label won't display. So in a pathetic desperate attempt, I put the code to display the label inside a button event to force it to display. Now it works perfectly (from the button click). But it still won't work from the boolean condition!
This is in viewDidLoad (note that the 'true' is just to isolate where the fault is):
if (true) [self showSubscribeLabel];
This is from the button click:
- (IBAction)askUserToSubscribe:(id)sender {
[self showSubscribeLabel];
}
-(void) showSubscribeLabel {
NSLog(#"here");
self.subscribeLabel.hidden = NO;
[self.view bringSubviewToFront:self.viewUnauthorized];
}
I see the output here when the controller loads and then again after clicking the button. What am I doing wrong? Thanks!

You should move your test and method invocation to viewWillAppear - often things aren't fully initialised in viewDidLoad. Having the code in viewWillAppear will also ensure that the condition is evaluated every time the view controller appears (such as when another view pops off a navigation stack) rather than when it is loaded for the first time.

Related

UITextField endEditing:YES (force) Not Dismissing Keyboard

I have several UITextField objects in a custom Date/Time editor control (no spinners). The control is in a UITableViewCell on a modally presented TableViewController during a data record add operation.
If users cancel the add operation I need to discard all the data and dismiss the controller and the keyboard if it is showing. I do this by calling this method when the Cancel button is pressed:
-(void)cancel
{
_tempMOC = nil;
[self.view endEditing:YES];
[self dismissViewControllerAnimated:YES completion:NULL];
}
According to the Apple docs, for UIView endEditing which state:
Specify YES to force the first responder to resign, regardless of whether it wants to do so.
my textFieldShouldEndEditing: return value should be ignored, however this is not the case.
I have verified with log statements that textFieldShouldEndEditing: is getting called.
But, the value is not ignored because only when this method returns YES is the keyboard dismissed.
The result is that the modal view controller gets dismissed leaving an orphaned keyboard on the screen which can only be gotten rid of by restarting the app.
This is a problematic pain in the arse because I do various validation operations in here:
-(BOOL)textFieldShouldEndEditing:(UITextField *)tf
{
/* Validate data for up to 6 UITextFields in this control. */
...
return valid ? YES : NO;
}
In the case where the user hit cancel on the controller, I really need it to ignore the return value, else I have have to do some kind of stupid caveat to figure out that six levels up somewhere the user hit cancel and in that case always return YES.
Any thoughts on what might be going on or if I have missed something... or ugh, I've found yet another Apple bug?!
****** UPDATE ****
This is now reported as radar: 32442632.

How to reset ViewController on the press of a button in iOS

My app has a ViewController consisting multiple textfield and custom UI elements arranged as a form with "submit" and "reset" buttons.
I want to reset all the textfields and custom UI elements when user clicks the submit button so that user gets a feeling that same Form is opened again.
I tried calling ViewDidLoad() and setNeedsDisplay() on the click of "Submit" button but data previously filled by user remains as it is.
Kindly Help!
Just set the text of all textfields to an empty string (the text value). There is no magic function to clean everything in a view.
The only other solution would be to actually display the ViewController again, but this is probably not what you want, because it causes overhead as well as you might see something of the switching on the screen.
If you programmatically coded your textfields and some other objects, you can refresh always your viewController everytime by adding all your UI codes in the predefined method: -(void)viewWillAppear:(BOOL)animated{} and you should remove all self.view elements by adding this code.
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
You codes in the viewWillAppear will look like this.
-(void)viewWillAppear:(BOOL)animated{
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Codes for User Interface here
}

Search freezes in UISearchBar with searchDisplayController

I have a UISearchDisplayController working nicely with a UISearchBar. shouldReloadTableForSearchString correctly returns results as the user types, but when the user hits the Search button on the keyboard and searchBarSearchButtonClicked is triggered, something odd happens after the final search results correctly display and the user hits Cancel on the search bar. Once the user reactivates search, the search opens but the cursor doesn't blink, the search results don't display and the keyboard looks locked on every keypress as shown in the attached image. There should be results for "Test", but nothing is happening and something seems to be held up b/c the last pressed key ("T") isn't going back down to the keyboard.
I'm holding off on including code b/c there are a lot of moving parts, but if any code would help, i can quickly include it. Thanks for your help.
UPDATE: it's not just after the user submits the query - if they've touched the resulting searchDisplayController.searchResultsTableView after inputting a query to scroll down through the results, and then hit Cancel, they'll see the frozen cursor and broken search once they activate the searchBar again (same image applies).
SECOND UPDATE: this only happens when I hide / unhide the navbar while search is active in viewWillLayoutSubviews. Any ideas? Really appreciate any help!
-(void)viewWillLayoutSubviews {
if (self.searchDisplayController.isActive && !profileSelected) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
LAST UPDATE: when hide/unhide code is placed into searchDisplayControllerWillBeginSearch and searchDisplayControllerWillHideSearch respectively, the searchBar incorrectly unhides 20px below navbar:
fixed it by adding this code:
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.searchDisplayController.searchBar becomeFirstResponder];
}
UISearchDisplayController is using UITableView to show search results. Whenever you scroll or new cells are loaded, viewWillLayoutSubviews is getting called. This is causing the app to freeze. Try moving you setNavigationBarHidden code in a separate method triggered by some other action.

array's objects loss in storyboards's scene

I make a game that has 2 scenes (first-welcome screen with "start game" button, second - is the game") Game is simple: head-picture is trying to prevent collions with bullets(represented as uiimageviews in NSMutableArray *bullets) with the help of -(void)touchesMoved:withEvent:. If it collides UIAlertView appears to get user's choise: repeat or go to wellcome-scene. If we go to the game-scene first time then everything is OK.
The problem is when we go to the game-scene next time. the property bullets after initiation in viewDidLoad shows its count as 3(as it should), but latter it shows bullets.count == 0;
I don't know how is it possible - I initiate this array in method that calls only in viewDidLoad. And in the first time everything works properly.
P.S. duaring code I don't use propertyName, only self.propertyName.
P.P.S I suggest reason in [UIView commitAnimations] - that's how I make bullets animation.
If I //hide it. evetything is OK. But without animation it looks poor.
Instead of calling the method in viewDidLoad, try calling it in viewDidAppear to set the count to 3 each time the view appears, because it should only be loaded into memory once, but viewDidAppear is called each time the view will show.

UIButton Not Hiding

I use IB and have properly wired up my buttons. I have verified they are properly wired, as some IBAction methods will have buttons hide properly. The issue I have is hiding the UIButtons when it first loads the app. In viewDidLoad I set the button property to hidden, but it doesn't hide it. Thoughts?
- (void)viewDidLoad {
stop.hidden = YES;
play.hidden = YES;
[activity startAnimating];
[super viewDidLoad];
}
After more debugging, the stop button hides but not the play.
It's hard to tell even from the code you posted why this is happening, The best guess will be that somewhere in the next lines of code you set it back to visible = YES mistakably. I would check if there is a method that shows the button that is called before it is needed.
BUT
If the initial state is hidden for your buttons. why don't you simply hide them on IB interface?
Try this
Remove outlet run the code ,test weather it works fine
Remove outlet,add hidden property via IB and run it again and test if it is seen properly.
If it works fine the problem is with code written.
Check the hidden property written via code

Resources