how to call function from other VC? - ios

I have some variable lets say isMenuVisible = false; I want to set some function when this var is changed:
isMenuVisible: Bool!{
didSet{
callFunctionFromOtherViewController()
}
}
How is that possible? Do I need to create instance of VC in order to access that function? Or I need to make that function public?

To call a method/function from another VC (or from any other class), you have 2 choices:
Create a class method in this other view controller:
In your MyViewController.h
+(void)myClassMethod;
Wherever you need to use it
[MyViewController myClassMethod];
Create an instance method in this other view controller:
In your MyViewController.h
-(void)myClassMethod;
Wherever you need to use it
MyViewController *myViewControllerInstance = [[MyViewController alloc] init];
[myViewControllerInstance myClassMethod];

definitely no need to create instance for the viewController,
if it is a rootViewController you can get it like that :
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var vc = (appDelegate.window?.rootViewController as? MyViewController)!
if your view controller is parent, it is like that:
var myViewfromParent = self.parentViewController as? MyViewController
if you use storyboard with id, you can get it like that:
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
hope it helps

If you're using viewController containment and you can easily reference the view controller you can simply create a public method in your .h file and call that method... however, with viewControllers it's often the case that a. you don't have / want universal access to the viewController instance in other classes and b. the viewController may not even exist when this method is changed. For these reasons I'd go with NSNotifications when triggering actions in viewControllers.
NSNotificationCenter addObserver in Swift
If this isn't acceptable and you can guarantee that this class will be linked in an architecturally sound way, I would create a delegate. That way you can set the delegate when you create the viewController and there's no mess when trying to get back to your viewController... I'd only recommend this if there's a genuine connection between the viewController and the class that contains the action (in this case the bool)
In objective c you'd do
MyViewController *myViewController = [[MyViewController alloc] init];
myViewController.delegate = theClassContainingTheBool; // could be self.. might not be... If you can't do this line of code, then you should use notifications
In swift
Delegates in swift?
How to define optional methods in Swift protocol?

Related

Get the presenting viewController of viewController.view in swift

i am trying to get the presenting viewController of a viewController's view
The idea is like that :
i have a
viewController = CategoriesViewController
and i am presenting its view inside anther
viewController = HomeViewController
by using
CategoriesViewController.view
so when i want to reach the
HomeViewController from CategoriesViewController
i do this
let vc = self.presentingViewController as? HomeViewController
but it is telling me that it is nil
i tried the
.parentViewController
and it is returning
CategoriesViewController
In case you want to change a variable in HomeVieController using CategoriesViewController you could create your own protocol. You can use protocols to communicate between different controllers.
protocol ChangeVariableProtocol {
func changeVar(variable: Int)
}
In the protocol itself you only declare methods.
In your CategoriesViewController you would create a delegate Varibale like this
var changeVarDelegate: ChangeVariableProtocl?
Whenever you want to change the variable in CategoriesViewController you call your protocol method.
changeVarDelegate?.changeVar(10)
In HomeViewController you need to implement this protocol and initialize the changeVarDelegate variable.
extension HomeViewController: ChangeVarProtocol {
func changeVar(var: variable) {
// Implement your own logic here
self.valueToChange = variable
}
And make sure that you initialize changeVarDelegate when you are instancing your CategoriesViewController.
Hope this helps!
Create a callback closure property both in CategoriesViewController and in its view.
Right before presenting the controller assign the closure to change the value of a property to the callback property in the controller.
In viewDidLoad hand the closure over to the view.
Call the closure in the view to be executed in the presenting controller.

How to call NavigationController within class, not derived from UIViewController?

My service class hasn't derived from UIViewController, but I'd like to call NavigationController to move to another controller.
class AppoxeeService
{
public void ShowInbox(){
..
//this code doesn't work (of course),
//because class isn't derived from UIViewController
NavigationController.PushViewController(new AppoxeeInboxController(), true)
//this code is mostly possible,
//but the NavigationController is null here
UIApplication.SharedApplication.KeyWindow.RootViewController.NavigationController.PushViewController(new AppoxeeInboxController(), true)
}
}
The Xamarin documentation is described well only for storyboards, but I'd like dynamically find current RootViewController and bind it to NavigationController.
In other simple (aka "HelloWord") samples from Xamarin or their documentation it is possible to instantiate from FinishLaunching method:
var window = new UIWindow (UIScreen.MainScreen.Bounds) {
BackgroundColor = UIColor.White
};
rootViewController = new RootViewController ();
navigationViewController = new UINavigationController (rootViewController);
window.RootViewController = navigationViewController;
window.MakeKeyAndVisible ();
I'm fully stucked with NavigationController and its usage from code.
You don't need to write complex code.
just call your instance of Navigation Controller that you declared in AppDelegate class
In AppDelegate.h
#property(nonatomic,strong) UINavigationController *navigationController;
In AppDelegate.m
#synthesize navigationController
then your code in didLaunch Method
in your class
just call the instance anywhere
[(((AppDelegate *)[[UIApplication sharedApplication] delegate]).navigationController pushViewController:yourViewController animated:YES];

Instantiate View Controller from Storyboard vs. Creating New Instance

What is the functional difference between instantiating a View Controller from the storyboard and creating a new instance of it? For example:
#import "SomeViewController.h"
...
SomeViewController *someViewController = [SomeViewController new];
versus
#import "SomeViewController.h"
...
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SomeViewController *someViewController = [storyboard instantiateViewControllerWithIdentifier:#"SomeViewController"];
In either case, is someViewController effectively the same thing?
The main difference is in how the subviews of your UIViewController get instantiated.
In the second case, all the views you create in your storyboard will be automatically instantiated for you, and all the outlets and actions will be set up as you specified in the storyboard.
In the first, case, none of that happens; you just get the raw object. You'll need to allocate and instantiate all your subviews, lay them out using constraints or otherwise, and hook up all the outlets and actions yourself. Apple recommends doing this by overriding the loadView method of UIViewController.
In the second case, the view controller will load its view from the storyboard and you will be happy.
In the first case, it won't. Unless you've taken other steps (like overriding loadView or viewDidLoad or creating a xib named SomeViewController.xib), you'll just get an empty white view and be sad.
In Swift you can do the same with,
var someVC = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewController") as! SomeViewController
You will need to give the Identifier in the Storyboard to the SomeViewController and tick the checkmark to Use Storyboard ID
It is not the same thing. In the storyboard you probably have some UI elements laid out. They might have constraints and properties setup through the storyboard. When you instantiate the viewcontroller via the storyboard, you are getting all the instructions for where those subviews are and what their properties are. If you just say [SomeViewController new] you are not getting all the instructions that the storyboard has for the view controller.
A nice test will be to add a UIViewController to a storyboard and drag a red view onto it. Instantiate it using both methods and see what the differences are.
simple swift 3 extension
fileprivate enum Storyboard : String {
case main = "Main"
}
fileprivate extension UIStoryboard {
static func loadFromMain(_ identifier: String) -> UIViewController {
return load(from: .main, identifier: identifier)
}
static func load(from storyboard: Storyboard, identifier: String) -> UIViewController {
let uiStoryboard = UIStoryboard(name: storyboard.rawValue, bundle: nil)
return uiStoryboard.instantiateViewController(withIdentifier: identifier)
}
}
// MARK: App View Controllers
extension UIStoryboard {
class func loadHomeViewController() -> HomeViewController {
return loadFromMain("HomeViewController") as! HomeViewController
}
}
In case you don't want to instantiate a new VC using instantiateViewControllerWithIdentifier but accessing the instance created by the storyboard from the AppDelegate:
create a property in AppDelegate.h so it will be accessible from classes using it
#property (nonatomic, strong) myViewControllerClass*vC;
in viewDidLoad inside myViewControllerClass.m I access the shared instance of AppDelegate and feed the property with self: [AppDelegate sharedInstance].vC = self;
I had to use this solution in a complex storyboard and still can't get over the fact that I cannot find an easy way to access all (or at least the ones I need) objects in storyboard simply by addressing their identifiers.
another thing to check for is if the viewcontroller that's throwing the error has a storyboardIdentifier, you can check the storyboard xib file.
the identifier was missing in my case, the error stopped when i added it

Get the UIViewController in TableView's CustomCell

I have one UIViewController named "MainView" and that has one UITableView named "tblLists"
tblLists generating the customCell - "customCellList".
My question is can I get the MainView's instance(self) in customCellList class.
I tried with superview thing but not get MainView. I want to achieve this without protocol.
So need your help in this.
You can use the responder chain to gain access to the view controller. Assuming your customCell class is a UITableViewCell subclass, the following method should do the job:
#implementation customCell
- (UIViewController *)getViewController
{
id vc = [self nextResponder];
while(![vc isKindOfClass:[UIViewController class]] && vc!=nil)
{
vc = [vc nextResponder];
}
return vc;
}
#end
The above code is courtesy of the Sensible TableView framework.
What you are asking is not a good idea, you should find another way around. It breaks the MCV pattern.
By the way, if you are worried with memory concern using ARC and targeting iOS>=5 you can create a weak reference to the table view itself and get the view controller as its delegate or data source property (of course if the VC is one them). Or you can create a weak reference to the VC itself.
As pointed in the comments is not a good idea, better find another way around. If you need to update you cells value there are a lot of methods to reload tableview data! By means of using KVO, notification, delegation etc on your VC from the model, you can simply trigger a reload to the table view without involving weird references in cells.
Hope this helps.
For those rare times when you want to break MVC.. This assumes you are using a Navigation controller as the rootVC on your window. Updated for Swift 2
func visibleVC() -> UIViewController? {
if let navVC: UINavigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as? UINavigationController {
if let vc: UIViewController = navVC.visibleViewController as? MyViewControllerClass {
return vc
}
}
return nil
}
Also you can access to rootViewController:
UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
I have similar question, my case is to change array in View Controller when the value of textfield in Table View custom cell changed.
My solution is add delegate for UITextFiled in cellForRowAt method of tableview, then I can do all my data changing in textFieldDidEndEditing method. Because they are all in one class, the ViewController Class.

Passing data between two different storyboards

I have two questions.
1. First question:
In my project, I have two different storyboard files: A storyboard and B storyboard.
I want to pass data (nsstring) from (A) controller of (A) storyboard to (B) controller of (B) storyboard
how I make it?
2. Second question:
In the second storyboard, I have two controllers chained with a segue.
When I call segue in the code with this instruction:
[self.navigation controller performSegueWithIdentifier: #"secondViewSegue" sender:self];
I have a message: "has no segue with identifier 'secondViewSegue' "
Why?
1/ A good way to do this is to make a separate model object that can be equally addressed from both locations. And the simplest way to do that is to add a property to the #interface section of your AppDelegate.h file, eg
#property (nonatomic, strong) NSString* sharedString;
To set and get it, you need to add typed access to the AppDelegate in any file that needs it:
#include AppDelegate.h
Then you can use...
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//to set (eg in A controller)
appDelegate.sharedString = string;
//to get (eg in B controller)
NSString* string = appDelegate.sharedString;
As an alternative to a property, you could use a static variable in a header file:
static NSString* staticString;
Which would be accessible to any object that #imports the header file. Not really the Objective-C way though.
For more elaborate cases, you may want to create a singleton object to access your model data.
2/ Try:
[self performSegueWithIdentifier: #"secondViewSegue" sender:self];
Ensure that the Segue is wired from your viewController, not it's Navigation Controller.
There are various ways to pass data between two different view controller in different storyboard. What I am using, I would like to share
Swift 3
let storyboard = UIStoryboard(name: "StoryboardName2", bundle: nil)
let controller :NextViewController = storyboard.instantiateViewController(withIdentifier: "NextVCIdentifier") as! NextViewController
controller.NameofUser = Self.SelectedUser
Declare variable in next viewcontroller as
public var NameofUser:String = ""

Resources