Run class viewDidLoad method from push button - iphone

I was wondering how can you load a class in objective-c from another class? I have a push action button in one and when the users presses this I want it to start this other Class (calling its viewDidLoad method)

You need to be more precise.
What kind of classes are you manipulating ? UIViewControllers ?
If so, you can alloc/init (or alloc/initWithNibName if you are using Interface Builder) the second ViewController when your button is pushed. The viewDidLoad method will be called when the view of the second view controller will be loaded (not necessarily when the viewController is allocated, but when the view will be displayed for the first time).
If you need the execute the code in the viewDidLoad method every time the button is pushed, prefere using the viewWillAppear or viewDidAppear methods.

If you are talking about the classes which are subclass of UIViewController then you can load view of another view controller by pushing it into the navigation controller object. Also you can add the view of the second controller on the first controller view.

Related

Manuel segue 'Show' does not call ViedDidload when returning to parent viewController?

I have a viewController containing a UITableViewController. This UITableViewController is populated by an array. When User presses populated cells they segue with a segue set to 'Manuel Segue : Show' this creates a back button UINavigation bar item. Inside this viewController the user is able to add Items to the array populating the parent UITableViewController. The problem is that when i segue back using the UINavigationbarItem it does not call viewDidload on the UITableViewController, there by not updating the UIViewTableCells. I have to close application to make it call viewDidload... How do i make it call ViewDidload when returning from the Manuel segue show? All help appreciated.
The pushing view controller is not unloaded when another view controller is shown above. As viewDidLoad: is only called once in the view lifecycle, this would then not be called when the segue is unwound.
Updating your tableview in viewWillAppear: or viewDidAppear: would cause this to be called whenever this view is displayed.
viewWillAppear would perhaps be better if you don't want to show the user the table reloading when not needing to asynchronously load data.
Read more on iOS view controller lifecycle here:
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html#//apple_ref/doc/uid/TP40015214-CH6-SW3

Access UI components Inside controllers init method

I'm trying to hide a button inside the controller's init method. But its not working. I have created a outlet to the image to access it from the controller. Dont know why the image is not get hidden.But I can do it inside the viewdidload method. Is it because the view still not loaded?
Yes the view will be loaded when viewdidload is called. so any changes to the existing UI should be called in view did load or view will appear
The UI components you're talking about are all subviews of the UIView that is owned by the UIViewController that you're creating. But that UIView and its subviews aren't actually created at the time the init method is called, so you can't change them there.
viewDidLoad is the appropriate place for any code that only needs to run once after the view has been created, but before it has been displayed.
viewWillAppear: and viewDidAppear: will run every time the view is about to or just did become visible again respectively.
Actually, the whole view hierarchy will be loaded when the loadView message is sent to the controller. This message is also sent automatically the first time you access the controller's view. That's why your message to the button gets discarded in the init method, because the object is not yet 'unfrozen' from the xib/storyboard. And yes, the appropriate place to do your view customization would be inside the viewDidLoad delegate method.
PS. Also... if you want the button to be hidden upon initialization, why don't you just check its hidden property in your xib/storyboard ?

When is viewWillAppear called?

By my count, the only two instances when viewWillAppear is called is when you initialize your view controller, or when you pop off the view controller that's on top of it on the navigation stack (ie pushing the back button on the viewcontroller ahead of it). Are there any other instances when viewWillAppar is called? I don't believe it's called when the app becomes active. Interested to hear some responses on this.
viewwillappear method is called as and when the view controller's view is added to the window. ( if the view is already in the window and is hidden by another view, this method is called when the view is once again revealed). The method is a notification to the view controller that the view is about to become visible. You can override this method to make any customizations with presenting the view.
This will also be called anytime addSubView is called, with your view.

How to pop to rootview controll without using popToRootViewControll method?

How to pop to rootview controll without using popToRootViewControll method because this method does not implement viewDidLoad method of RootViewController???
What do you mean by "because this method does not implement viewDidLoad method of RootViewController"?
The "viewDidLoad" is called only one time if you don't have memory warning. If you get an memory warning your viewDidLoad method is called again.
You need to implement your logic in "viewWillAppear".
And maybe you should read View Controller Programming Guide for iOS
UIViewController Class Reference here you find the viewWillAppear, and viewDidAppear methods
I think you need to take a different approach to solving this problem.
If you have code in the root view controller's viewDidLoad method that you want to run again, then that code shouldn't be in the viewDidLoad method in the first place. viewDidLoad only runs just after the view controller's view is loaded (usually just once in the lifetime of the view controller). You should probably put that code it in its own method in the view controller, so you can still call it from the viewDidLoad method.
To have the code run when you pop to the root view controller, you can call that new method from the root view controller's viewWillAppearAnimated method, which gets called each time the view controller's view is about to be displayed.

viewWillAppear only being called once

Here's the scenario, switchViewController is the view added to the main window. So switchViewController is the main view, so if I want to go view B, I will addsubview of view B, there isn't a need to remove switchViewController's view right?
The issue is after I go back from view B to switchViewController's view, the method viewWillAppear is not being called anymore.
Why is it so?
viewWillAppear: is not called automatically when a view is removed from or added to the view hierarchy. It is the responsibility of the view controller to call it at the right time. The built-in view controller classes do this whenever you present or push a new view controller. Since you do not use this mechanism in your app, the method doesn't get called (unless you call it yourself).
That's because it never disappeared, you were just putting something else in front of it. If you want to navigate from one screen to another and back, they should be separate view controllers, and you should be using UINavigationController and its pushViewController:isAnimated: method.
It's not getting called beause your just modifying the first view, not navigating to a different one.
You might consider embedding your view in a Navigation controller, then calling your ViewB with
[navigationController pushViewController:viewB animated:YES];