viewDidLoad issue - objective-c

I am having problem with ViewDidLoad Method.I have one .Xib File in my app and 5 viewControlelr.And Each View Controller Contains some initialization and method calls.
Problem is at the startup all the ViewDidLoad method runs.
Is there any way i can do this just when my ViewController will be called and the View is Loaded on the screen?
What is the proper way in this case?
I tried like this code :
-(void)viewDidAppear
{
[ScoreWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://smwebtech.com/Pandit/web_service/getScore.php?u=aa"]]];
}
But my program is not reaching this method.Why?Am i missing something?Please take a look.

Use viewWillAppear method instead. It is only called when the view is going to appear on screen.
Or viewDidAppear which is only called after the view appears on screen.
EDIT:
Both viewWillAppear and viewDidAppear take a BOOL argument each:
- (void)viewWillAppear:(BOOL)animated {
}
- (void)viewDidAppear:(BOOL)animated {
}
EDIT2:
These two delegate methods have their corresponding 'opposite' methods that you can override to release resources you allocate in the former methods:
- (void)viewWillDisappear:(BOOL)animated {
[resource release];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[resource release];
[super viewDidDisappear:animated];
}

Related

ViewControllers with TextViews in UIPageViewController

I was trying to learn UIPageViewControllers and hit an Issue which I couldn't resolve.
This is what I tried to do:
Steps:
I simply created 2 view controllers and a page view controller in
StoryBoard.
Then I added some code to the File's Owner of PageViewController to
behave as a dataSource and delegate to itself.
When I ran, things worked well.
I added some buttons, and text fields to the second view controller.
I ran, worked well.
Now I added a text view to the second view controller and ran. When I tried to write something inside the text view, the page control jittered and moved to first view controller.
Has anyone experience this ever?
#interface AMPageViewController : UIPageViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
#end
The implementation:
#import "AMPageViewController.h"
#interface AMPageViewController ()
{
UIViewController *mainController;
UIViewController* socController;
}
#end
#implementation AMPageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
mainController = (UIViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"First"];
socController = (UIViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"Second"];
[self setViewControllers:#[mainController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
self.dataSource = self;
self.delegate = self;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if (viewController == socController )
return mainController;
else return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if (viewController == mainController )
return socController;
else return nil;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return 2;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#end
If you want to download and try the project
I've investigated a lot on this problem.
It seems a bug related to the internal (private) UIScrollView of the UIPageViewController.
If you search on StackOverflow you will find a lot of post with this problem and no solutions...
I seems that the UITextView (which is an UIScrollView and, AFAIR, has an internal UIWebView), sends some strange message to it's superviews chain, that makes the private UIScrollView of the UIPageViewController scrolling to the top-left corner.
I would have tried to block this message using method swizzling, but this is probably not ok for AppStore. So I tried other things.
The final solution is very simple: simply, embed your UITextView inside an UIScrollView!
This is a link to your project updated
If you do so, you'll solve the problem!
Try and let me know
EDIT:
How did I arrive to this solution:
An intuition.
A lot of debug and stack traces had make me think that the problem was related to a bug in the "nesting UIScrollView" system and some messages sent from the inner view to its superview.
UITextView inherits from UIScrollView and has inside an UIWebDocumentView (private) which is another UIScrollView. During the debug I saw a lot of messages (private methods) like "relayout superview" sent to the upper UIScrollView's. So, for some reason, the inner scroll view (UIWebDocumentView?) was sending a message/event to it's superview. This message/event (probably because of a bug) was not stopping to the external UITextView, and was forwarded to the UIScrollView handled by UIPageViewController.
Embedding the UITextView inside a simple UIView was not enough, because UIView forward the message to it's superview if it can't handle.
I thought: UIScrollView probably doesn't (otherwise it wouldn't simple to nest UIScrollViews), so I tried and it worked.
This is all a supposition because I stopped inspecting, I will have a more in-depth look this week.
Build target iOS-7.0.
The scrollview trick wasn't working for me. Tried to embed the textview in a scrollview through storyboard and code but no luck.
Simply delaying the call to the textview did it. Not very elegant, but its the only thing I've gotten to work so far.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.textView becomeFirstResponder];
});
}
Tested, working on my iPhone 5 and my ultra-slow iPhone4. Although its totally possible that whatever implementation detail enables the textview to become the responder could take longer than the set time. So keep in mind this isn't exactly bulletproof.
--EDIT--
Well... it's working on my iPhone 4 beater with a delay of 0.0000000000000001
you did not set before and after view controllers and also look in to first responder for socController

Objective-C how to call a method after self is initialized inside the object file?

Is there any way to know when a custom object is finished with being initialized from inside the object's file? Or let me rephrase the question, why can't I call any method inside this method?
- (id)initWithCoder:(NSCoder *)coder {
//NSLog(#"initWithCoder inside CustomObject (subclass of UIView)");
self = [super initWithCoder:coder];
if (self) {
//... initialization here
[self visibleEmptyButton]; //why does this method never get called?
}
return self;
}
EDIT:
-(void)viewDidLoad{
NSLog(#"viewDidLoad inside CustomObject(subclass of UIView) is called"); //It never gets called
[self viewDidLoad];
//initialization here...
}
(If the class you are init-ing is a subclass of UIViewController) Changing and setting things in the screen should be done after the view is loaded. Try doing it in this method:
- (void)viewDidLoad {
[super viewDidLoad];
[self visibleEmptyButton];
//Do the additional view altering here
}
If this method doesn't exist yet you can just add it to the .m file (no need to add it to the .h file).
In lieu of you're edit you could simply move the call to the UIViewController:
- (void)viewDidLoad {
[super viewDidLoad];
[TheInstanceOfYourViewClass visibleEmptyButton];
}
Also, to avoid making a whole bunch of small subview related methods public it often makes sense to create one method to handle the initial visual states.

Cannot dismiss the Search view

I have a parent class with tableview and searchbar over it which is a subclass of tableview controller. Delegates for the searchBar and searchdisplaycontroller are set in a seperate class inherited from UISearchdisplaycontroller. The datasource and delegates for tableview and searchbar are handled in this class seperately. The classes are under ARC.
Hence, When a user taps on search, the control transfers from FilesListController (parent)class to this class. Now, When a user taps on cancel button, the searchbar delegate set in this class i.e.
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
is CALLED but DOESN'T serve the purpose of dismissing the full screen searchtableview and return to the parentviewcontroller. However, if I don't write this delegate in the search class, it works properly. I have set the searchbar delegates in xib and on calling:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
like this:
self.searchResultsTableView.delegate = self;
self.searchResultsTableView.dataSource = self;
[parentFileViewController.searchDisplayController setDelegate:self];
Where am I going wrong? Thanks in advance.
If you want to dismiss a UISearchBar with a SearchBarController, just use this Code:
[self.searchDisplayController setActive:NO animated:YES];
you should implement resign the responder in the delegate function i.e
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
[searchBar resignFirstResponder];
}
Memory warnings can appear at any time during the application run time, you must assume a memory warning will happen and the view and disposable objects will have to be recreated.
We are handling such situation by setting to nil our arrays:
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if([self isViewLoaded] && self.view.window == nil)
{
self.view = nil;
keys = nil;
names = nil;
errorDuringNetworkCall = nil;
}
}
And by dismissing the search bar tableview before performing the segue operation:
[self performSegueWithIdentifier:#"navigateToNextScreen" sender:self];
self.searchBar.text = #"";
[self.searchDisplayController setActive:NO animated:YES];
After a Memory warning is received the viewDidLoad method is called again and the arrays are populated, the search bar will continue to be useful.work without issues

viewDidDisappear not getting called on a UINavigationController

I'm having a problem with my navigation controller. If I add a view controller to the stack:
- (void) tui_ToggleListStudy:(id)sender
{
listVC = [[ListViewController alloc] init];
[self.navigationController pushViewController:listVC animated:NO];
[listVC release];
}
I have NSLog messages for the view controller beneath, for both viewWillDisappear: and viewDidDisappear - but only viewWillDisappear: is getting called.
Not only that, but the view controller doesn't receive any other delegate messages either: No viewDidUnload, or dealloc...
Is there anything I can do about this?
I'm stumped! Any thoughts?
Thanks!
I know the answer if you made the same typo in your code that you made in your question: the method signature is viewDidDisappear: (with the animated argument), not viewDidDisappear.
Not only that, but the view controller doesn't receive any other delegate messages either: No viewDidUnload, or dealloc...
A view controller will not be deallocated when you push another controller onto the stack. And viewDidUnload won't be called unless you run out of memory.
Assuming your navigation controller is contained in some kind of top view controller, you must also forward the relevant messages from that top view controller to the nav controller:
-(void)viewWillAppear:(BOOL)animated
{
[navController viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated
{
[navController viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated
{
[navController viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated
{
[navController viewDidDisappear:animated];
}
You must call super at implementation of viewWillDisappear.
The designated initializer for UIViewController is -initWithNibName:bundle:. Are you sure your view controller is finding its nib and finds its connected view? I'll bet if you set a breakpoint after init'ing your ListViewController, you'll find its -view returns nil.

viewDidLoad in NSViewController?

On the iPhone I use UIViewController's viewDidLoad to run code to set up the view.
How can I do that with NSViewController?
I've tried loadView but it doesn't work...
I figured it out within minutes of posting my comment. Adding my finding as an answer because it is an example which is missing in the docs. The below code will give you the viewDidLoad method that you want. Its so easy in a way that i wonder why Apple has not implemented it yet in OS X.
- (void)viewWillLoad {
if([NSViewController instancesRespondToSelector:#selector(viewWillLoad)]) {
[super viewWillLoad];
}
...
}
- (void)viewDidLoad {
if([NSViewController instancesRespondToSelector:#selector(viewWillLoad)]) {
[super viewDidLoad];
}
}
- (void)loadView {
BOOL ownImp = ![NSViewController instancesRespondToSelector:#selector(viewWillLoad)];
if(ownImp) {
[self viewWillLoad];
}
[super loadView];
if(ownImp) {
[self viewDidLoad];
}
}
Original source: http://www.cocoabuilder.com/archive/cocoa/195802-garbage-collection-leaks-and-drains.html
As of OS X 10.10, viewDidLoad is available and supported on NSViewController.
Prior to that, you had to go by this nugget in Snow Leopards' release notes:
Advice for People who Are Looking for -viewWillLoad and -viewDidLoad Methods in NSViewController
Even though NSWindowController has -windowWillLoad and -windowDidLoad methods for you to override the NSViewController class introduced in Mac OS 10.5 does not have corresponding -viewWillLoad and -viewDidLoad methods. You can override -[NSViewController loadView] to customize what happens immediately before or immediately after nib loading done by a view controller.
As of OSX 10.10 (Yosemite), there is now a -viewDidLoad, -viewWillAppear, -viewDidAppear, -viewWillDisappear in NSViewController. See WWDC 2014 - Storyboards and Controllers on OS X session for more info, to find out when each of them gets called, etc.
Here's the relevant bit from the 10.10 header docs about -viewDidLoad:
Called after the view has been loaded. For view controllers created in
code, this is after -loadView. For view controllers unarchived from a
nib, this is after the view is set. Default does nothing.
- (void)viewDidLoad NS_AVAILABLE_MAC(10_10);
why don't you try this:
- (void)awakeFromNib {
//setup code
NSLog(#"hello there");
}
It looks like in 10.10, viewDidLoad is now in NSViewController.
hmm actually I would also do this...
- (void)viewWillLoad {
if (! bool_viewwillload) {
// execute the code
bool_viewwillload = true;
}
}
- (void)viewDidLoad {
if (! bool_viewdidload) {
// execute the code
bool_viewdidload = true;
}
}
and then just make the load view like this
- (void)loadView {
[self viewWillLoad];
[super loadView];
[self viewDidLoad];
}