objective-c: help me understand views and modal transition - iphone

I'm lacking a serious understanding of how modal transitions work, so please do not leave the simplest answer out of this.
I have two views with their own view controllers set up in storyboard. A button on the main-menu leads to other-view. I set up this transition purely via ctrl-click and selecting the modal transition. I also have a button leading from the other-view back to the main-menu, set up similarly.
To further my understanding of these transitions I decided that I want the main menu to play a sound when it loads up, but only the first time i hit run, and not again when i go hit the button the other-view to go back to menu-view.
in my menu-view I have a private property BOOL
#interface MainMenuViewController ()
#property BOOL menuSoundPlayed;
#end
and my viewDidLoad...
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.menuSoundPlayed){
//sound setup code omitted for clarity
AudioServicesPlaySystemSound(mySound);
self.menuSoundPlayed = YES;
}
}
can someone help me understand why the menu sound plays every time main-menu view loads? I do acknowledge that the menuSoundPlayed is never really initialized, but I dont know where I would even do that.
side-note: I love that apple gives us all these conveniences like story-board, but I almost wish it was easier to see all the code behind these things, so i could actually understand what was going on when i 'segue' between views.
Anyways, thank you!

I did a bit more researching and answered my own question.
In the situation I had before (described in my question) I had the code
[self performSegueWithIdentifier:#"mainMenuSegue" sender:sender];
being executed, which as commenters described, starts a new instance of my main-menu.
What i wanted was to return to the main-menu instance already created.
To do this the following code is necessary (with your own completion block obviously):
[self dismissViewControllerAnimated:YES
completion:^{
NSLog("view dismissed");
}];

Related

memory not releasing with ARC and storyboard in iOS 5.1

i'm cracking my head on memory issues with my app, the app are working fine except that it will crash once it hit low memory warning and are very very very laggy when using it for 10 to 20 minutes.
EDIT: how to poptoviewcontroller?
introvideo-> welcomeview & tutorialview-> mainviewcontroller-> scannerviewcontoller-> questionview ->(if answer correct -> correctView) else ->wrongView
how do i pop back to mainView controller ?
the below code are to solve adding view controller to the navigationcontroller.viewcontroller stack. As i'm using storyboard pushing from viewcontroller to another view contoller with out poping.
the code will pop to the viewcontroller that are already in the viewcontroller stack.
the flow of the of my storyboard as attached:
http://dl.dropbox.com/u/418769/storyboard%20flow.png
intro video -> welcome view & tutorial view (if username !exist) -> main view controller
this is the main file that user will alway go to.
http://dl.dropbox.com/u/418769/scannerViewController.h
http://dl.dropbox.com/u/418769/scannerViewController.m
i'm using a custom segue to pop viewcontrollers, which solved part of the problem.
-(void)perform {
UIViewController *sourceVC = (UIViewController *) self.sourceViewController;
NSInteger index = -1;
NSArray* arr = [[NSArray alloc] initWithArray:sourceVC.navigationController.viewControllers];
for(int i=0 ; i<[arr count] ; i++)
{
if([[arr objectAtIndex:i] isKindOfClass:NSClassFromString(#"mainViewController")])
{
index = i;
}
}
[UIView transitionWithView:sourceVC.navigationController.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[sourceVC.navigationController popToViewController:[arr objectAtIndex:index] animated:NO];
}
completion:^(BOOL completed)
{
}
];
}
however, the app are still eating up the RAM and VRAM.
I really appreciate any friends here to help solving my question, does Strong value caused this problem ?
When you say that you're using a "custom segue to pop to the Main View Controller", I'm not sure if I quite understand that. Are you using performSegueWithIdentifier? If so, then you're not popping; you're pushing another copy of the main view controller!
In most storyboards, you don't see a segue out of the right side child view looping back to the left of the parent view (and your screen snapshot showed a dizzying number of segue's entering back into the main view controller, which is a bit of a red flag). This is a far more customary storyboard (taken from Beginning Storyboards in iOS 5) at Ray Wenderlich):
(source: cloudfront.net)
Usually you'll dismiss a child view with something like the follow, not use a segue.
[self.navigationController popViewControllerAnimated:YES];
Of, if you wanted to pop back multiple levels, you would use popToViewController or popToRootViewControllerAnimated. Or if you use modal segues, you would dismiss the modal with dismissViewControllerAnimated.
If I've misunderstood what you mean't by "custom segue to pop", can you provide what code you're using do to that?
Computer-aided analysis is the way to solve this. Do Build and Analyze and resolve all issues. Run your app under the Leaks and Allocations instruments. Use heap-shot analysis.
#ken-thomases analysis is spot on. Also see Finding leaks.
I presume you are using ARC?
Can you explain what is the purpose of the above code sample and what your app is doing to require something like this? It feels like you're addressing a symptom rather than solving a problem.
In answer to your question, the use of strong is unlikely to be the source of problems, unless you have strong reference cycles (see Transitioning to ARC). If you follow Ken's suggestions, you'll identify where your app is leaking memory (assuming it is), and if so, where it is. At that point, if you still don't understand the source of the leak, you can post the offending code here and I'm sure there will be plenty of people willing to help. Also, if you have some code that you wonder whether the strong reference is inappropriate, post that relevant code and, again, I'm sure we'll be happy to help if we can.

What goes in the View and what goes in the View Controller?

I am new to programming for the iPhone and this will be my first question here. I have experience with different languages like php/java/c++.
My question is about ViewControllers and views in iOS.
I have started a project which will contain several different things like a login screen, a main screen and several other screens. The goal of this project is to learn how to create everything programmatically instead of using interface builder to get more accustomed to the system. I am using the book: "Advanced iOS 4 Programming" to help me.
I have been able to create all the screens ( and stuff like logging-in is working ), but I am not sure if I did it correctly.
All of my code for creating the textfields/labels/buttons is now located in the ViewController while the main view where everything is put on is almost empty, with nothing being done in it. Shouldn't the code to create the textfields and other components be located in the view itself, or is this the correct approach?
I have looked at several different examples but most use interface builder. The book itself is also not very clear in this matter.
Thanks in advance.
Kind regards,
Jasper
In the view you have the view - on other words, literally what the human user sees with their eyeballs.
So, for example, if you are doing complicated drawing you will have your own custom drawRect: method, and that for example is in the view.
On the other hands ......
In the view controller you have things that control the view.
Generally speaking "everything" goes in the view controller.
When you first start programming for iPhone (or Mac), simply put everything in the view controller and don't worry too much. There's plenty to learn. OK?
Eventually, separate out the "actual drawing" separately in to the view.
Hope this simple explanation for beginners helps!
In simple controller code should contain methods like...
class myLoginController : NSObject
{
UIView *myView;
}
-(void) initLoginController
-(void) loadLoginViewInView :(UIView*)inView;
-(void) removeLoginView;
-(void) isViewLoaded;
-(void) submitButtonClicked : (id) button;
-(BOOL) isLoginSuccess;
and initLoginController you can create your view,
-(void) loadLoginViewInView :(UIView*)inView
{
[inView addSubview:myView];
}
and in removeLoginView you can remove "myView" from its superView .

How to actually implement the paging UIScrollView from Apple's example?

I found an example of a paging UIScrollView in Apple's developer docs and it's just what I want for my application. I have a main view with a "Help" button that I want to present a view that users can page through to see all the help topics. The sample is at:
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html%23//apple_ref/doc/uid/TP40008179-CH3-SW1
And it works as advertised, but, as with so many of Apple's examples, they do all kinds of code right in the AppDelegate. I have my own AppDelegate, but then I have a NavigationController with a RootView that has this "Help" button on it. I can't seem to get the example code integrated into my own and have it work. I'm pretty sure I can't put the code they have in their AppDelegate in my own, but how to set it up eludes me.
Can someone point me to an example where they do what I'm talking about?
EDIT: I was able to create a new project and get it to work like Apple's by moving all the AppDelegate methods into the UIViewController that the template supplied and creating a new ContentPage UIViewController to hold the content. It can scroll from page to page, so I think I can insert this code into my other project ok.
I replaced the applicationDidFinishLaunching with an equivalent viewDidLoad and got rid of the AppDelegate stuff dealing with window and such. Then I changed Apple's initWithPageNumber: method to refer to my help pages rather than just creating instances of their generic views.
- (id)initWithPageNumber:(int)page {
NSLog(#"--initWithPageNumber:ContentPage");
if (self = [super initWithNibName:[NSString stringWithFormat:#"HelpPage%d", page] bundle:nil]) {
pageNumber = page;
}
return self;
}
Thanks for the help - sometimes it's good just to have someone tell you it can be done to keep going!
In the sample code, the applicationDidFinishLaunching: method sets up the UIScrollView (and a UIPageControl, but let's ignore that for now) and then "loads" the first two pages. "Loading" a page consists of loading that page's view controller into memory and caching it. It then adds that controller's view as a subview to the UIScrollView offset with an appropriate x based on what "page number" it is. You should be able to do all that in your viewDidLoad.
If you have a blank view when your view controller's view is shown, then you haven't added the subview correctly to your UIScrollView. Depending on what exactly you changed, that could be some combination of (a) not getting the x offset correct, (b) not setting the frame correctly, or (c) not adding it as a subview correctly.
If you post some of your own code we might be able to shed some light on the problem.

Objective-C/iPhone: Windows' view not responding to button clicks!

So in my app delegate I add a call add the myViewController.view to the main window:
1. [window addSubview:myViewController.view];
In myViewController I do the following code in the viewDidAppear method:
2. [self presentModalViewController: yourViewController animated: YES];
In my yourViewController class I do the following to try and go back to the main window
3. [self dismissModalViewControllerAnimated:YES];
My main windows view appears with buttons in all, but the buttons won't react to any click or anything. It's like there is something over them that I can't see.
Also, the main windows button works before this process but doesn't after the process.
Any help would be appreciated.
If the dismiss method call is in the modal view controller (not the parent that presents it), then you actually want to call [self.parentController dismissModalViewControllerAnimated:YES];
There are a number of reasons why things might not be responding to your touches. Here are two that have happened to me:
The frame of the view you want to touch is too small. UIViews can draw outside of their frames, so it might look ok, but not respond if the touch is technically outside of the frame -- you also have to check that all the superview's up the hierarchy also have a large enough frame.
If anything in your view is a UIImageView or child thereof, it won't respond to user touches because UIImageView has userInteractionEnabled set to NO by default. You can fix this just by setting myImageView.userInteractionEnabled = YES;
Edit: Oli pointed out in the comments that dismissModalViewControllerAnimated: should work if called on either self.parentController or simply self, since that method is smart enough to call the parent if needed, according to the docs. The docs also make it sound like this might behave differently if you have multiple model views open at once, though, so I would still consider it cleaner code to call the method on self.parentController directly.

iPhone UIViews sleeping/dying after being brought back into view

I need some help. This seems to be a common problem I am having when am adding and changing views in my coding. And I would love to learn what I am doing wrong.
Currently I am adding and removing views using the following calls from my view controller:
[startView removeFromSuperview];
[self addSubview:secondView];
and then doing the opposite again to go back.
[secondView removeFromSuperview];
[self addSubview:startView];
I am fine up to this point.
But the problem I have is that when I then decide to go back to 'startView" and call the first code that I have above for the second time.
My View loads but very little works.
None of my methods are called, there is no animation and the view is shown but it is "dead" or "asleep". And I have no idea why!
I am basically adding a view, removing it, then adding it again and everything breaks.
Can anyone give me a hand as to what might be happening? is it that ViewDidLoad doesn't fire the second time it's loaded? or something like that?
I would much appreciate it.
I may have figured it out So don't worry!
I had a flag hidden in my code somewhere that was stopping my methods from firing.
Sorry!