array's objects loss in storyboards's scene - ios

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.

Related

Set UIPicker in motion like a slot machine after a button pressed

I'm very new to objective-c and IOS development. I want to create a very simple slot-machine app. For that I used an UIPicker and filled it with the required data. However I'm having trouble to make it roll after the user clicks a button. I don't have any idea how to implement that kind of movement.
Can anyone give me a direction? I don't have any code to add because everything is displayed like it should, except that method to make the picker actually move which I don't know how to implement at all.
You can accomplish this by loop in animating to a specific row , so create a timer that fires every 1 second inside it increment row / component put this
[picker selectRow:row inComponent:component animated:YES];

Can't get UILabel to display from boolean condition

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.

Detect when UIView/UILabel redraws itself?

I have an app that fetches calendar events and displays data to the user. I'm getting some weird behavior when trying to update my labels.
I can fetch the calendar data just fine but when that gets done, my problem is that according to NSLog my label.text property has already changed, but it's another 4-8 seconds before the view gets redrawn.
Therefore, I'm trying to detect when the label gets redrawn, not when it's .text property changes so I can hide a progress view at the same time the data is populated in the labels.
I have already tried setNeedsDisplay and setNeedsLayout on self.view and the labels themselves. after the .text property of the labels has changed - doesn't work.
So unless I'm completely missing something about using setNeedsDisplay (which I understand only updates on the next redraw anyway), my question is, how do I detect when the UILabel and/or the UIView redraws itself?
How my app is setup:
I've been stuck on this for about 3 weeks.
Make sure setNeedsDisplay is being called on the main thread, using performSelectorOnMainThread:withObject:waitUntilDone:, for example:
[view performSelectorOnMainThread:#selector(setNeedsDisplay)
withObject:nil
waitUntilDone:NO];
Quote apple develop document :
The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.
maybe your main thread are blocking by other things , such as deal with many complex calculations
eg:
- (void)testMethod
{
myLabel.mytext = #"aaaa";
[myLabel setNeedsDisplay];
// some complex calculations
// the quickest , it will run finish the method then redraw.
}

NSTimer in a controller, how to notify the view?

In my iOS application, when the user pushes a button in a view, a NSTimer is trigered in the controller.
On the third tick, I would like to make the background of the view bliking.
I've written the blinking function in the view (it should't be written in the controller, should it ?)
I can trigger this blinking function in the controller by
LostView *lostView = (LostView* ) self.view;
[lostView blinkBackground];
But it's bad, isn't it ? The controller shoudn't know the view neither the name of the function ?
I would like to apply the MVC pattern
Is the observer/obervable pattern applicable in this situation ?
Thanks
No it's not bad at all. It looks like you implemented the method to make the view blink in the view itself. That's fine, because it's directly related to the visual representation (i.e. the view part of MVC). You could reuse that view in any other app that requires a blinking view.
Since that blinking is triggered by an NSTimer I assume that it's somehow dependent on the logic in this specific app. The view can't (shouldn't) know when it's supposed to blink (that would only be the case if that blinking was a direct reaction of an interaction with it or another related part of the UI - or it was part of a more complex element, for example a countdown timer that always starts to blink when it reaches the last 10 secs or so. For example the UIButton provides the possibility to highlight it self if it's touched.)
But if that blinking is a reaction of some state transition in your app, maybe some new data becomes available or a countdown is about to expire, the controller is a perfectly reasonable place to trigger that.

UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?

I have always been a bit unclear on the type of tasks that should be assigned to viewDidLoad vs. viewWillAppear: in a UIViewController subclass.
e.g. I am doing an app where I have a UIViewController subclass hitting a server, getting data, feeding it to a view and then displaying that view. What are the pros and cons of doing this in viewDidLoad vs. viewWillAppear?
viewDidLoad is things you have to do once. viewWillAppear gets called every time the view appears. You should do things that you only have to do once in viewDidLoad - like setting your UILabel texts. However, you may want to modify a specific part of the view every time the user gets to view it, e.g. the iPod application scrolls the lyrics back to the top every time you go to the "Now Playing" view.
However, when you are loading things from a server, you also have to think about latency. If you pack all of your network communication into viewDidLoad or viewWillAppear, they will be executed before the user gets to see the view - possibly resulting a short freeze of your app. It may be good idea to first show the user an unpopulated view with an activity indicator of some sort. When you are done with your networking, which may take a second or two (or may even fail - who knows?), you can populate the view with your data. Good examples on how this could be done can be seen in various twitter clients. For example, when you view the author detail page in Twitterrific, the view only says "Loading..." until the network queries have completed.
It's important to note that using viewDidLoad for positioning is a bit risky and should be avoided since the bounds are not set. this may cause unexpected results (I had a variety of issues...)
This post describes quite well the different methods and what happens in each of them.
currently for one-time init and positioning I'm thinking of using viewDidAppear with a flag, if anyone has any other recommendation please let me know.
Initially used only ViewDidLoad with tableView. On testing with loss of Wifi, by setting device to airplane mode, realized that the table did not refresh with return of Wifi. In fact, there appears to be no way to refresh tableView on the device even by hitting the home button with background mode set to YES in -Info.plist.
My solution:
-(void) viewWillAppear: (BOOL) animated { [self.tableView reloadData];}
Depends, Do you need the data to be loaded each time you open the view? or only once?
Red : They don't require to change every time. Once they are loaded they stay as how they were.
Purple: They need to change over time or after you load each time. You don't want to see the same 3 suggested users to follow, it needs to be reloaded every time you come back to the screen. Their photos may get updated... you don't want to see a photo from 5 years ago...
viewDidLoad: Whatever processing you have that needs to be done once.
viewWilLAppear: Whatever processing that needs to change every time the page is loaded.
Labels, icons, button titles or most dataInputedByDeveloper usually don't change.
Names, photos, links, button status, lists (input Arrays for your tableViews or collectionView) or most dataInputedByUser usually do change.

Resources