ViewController Delegate - objective-c

is it possible to access a ViewController's methods from the appDelegate, like it is possible inverse with the following code:?
AppDelegate *ad = (AppDelegate *) [[UIApplication sharedApplication] delegate];
When I try
ViewController *vc = (ViewController *) [[UIApplication sharedApplication] delegate];
I get an error...
Thanks!

In your AppDelegate, assuming viewController is your rootViewController, you can get a list of all view controllers on the stack by with:
NSLog(#"List of current active view controllers:%#", self.viewController.navController.viewControllers);
To access a specific view controller in the viewControllers:
[[self.viewController.navController.viewControllers objectAtIndex:i] someMethodOfThatViewController];

ViewController *vc = (ViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
or if your delegate have properties
YourAppDelegate *ad = (YourAppDelegate*)[UIApplication sharedApplication].delegate;
ViewController *vc = ad.yourViewControllerProperty;

Related

how can I use UINavigationController to switch between pages in Swift

I am new to swift development, I am trying to switch between UINavigation controller, I saw a snippet of code that performed this feature in Objective-c,
UINavigationController* webViewNavigator = [[UINavigationController alloc] initWithRootViewController:okraWebView];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
ebViewNavigator.modalPresentationStyle = UIModalPresentationFullScreen;
[delegate.window.rootViewController presentViewController:webViewNavigator animated:NO completion:nil];
I am trying to replicate it for swift. how do I achieve this?
You can try
let webViewNavigator = UINavigationController(rootViewController: okraWebView)
let delegate = UIApplication.shared.delegate as? AppDelegate
ebViewNavigator.modalPresentationStyle = .fullScreen
delegate?.window.rootViewController?.present(webViewNavigator, animated: false)

UIAccessibility on subview of UIWINDOW

Hi have added Viewcontroller view into UIWINDOWS as below
[[[UIApplication sharedApplication] keyWindow] addSubview:self.myviewcontroller.view];
But now for myviewcontroller view UIAccessibility is not working. Its getting whatever backside of that window.
I figured solution,
we should add subview on topmost window.
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:self.myviewController.view];

How do I access a TableViewController in app delegate?

Currently, I have a SplitViewController setup. Wherein the MasterViewController has a bar button, when pressed, a UITableViewController popovers.
I'm want to connect the delegate in UITableViewController to MasterViewController. Based on tutorials, most people do this in app delegate.
Here's my code in app delegate. I haven't added the TableViewController class yet.
UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
splitViewController.delegate = [splitViewController.viewControllers lastObject];
DetailViewController *detailViewController =(DetailViewController *) [splitViewController.viewControllers lastObject];
MasterViewController *masterViewController = (MasterViewController *) [[splitViewController.viewControllers objectAtIndex:0] topViewController];
masterViewController.delegate = detailViewController;
detailViewController.delegate = masterViewController;

how to call drawrect method from another class when it is made in appdelegate class

I have made a method in appdelegate class which is-
#implementation UINavigationBar (category)
- (void)drawRect:(CGRect)rect{
UIImage *img = [UIImage newImageFromResource:#"Img.png"];
[img drawInRect:rect];
[img release];
}
#end
I want to set the size(x,y,width,height) of the image(Img.png) programitcally in some other class..so how to call/use (- (void)drawRect:(CGRect)rect) method in some other class?
do like this
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate drawRect:CGRectMake(x,y,width,height)];

add many view controller

How can I receive different values from appDelegate?
#synthesize window;
#synthesize viewController;
#synthesize viewController2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:viewController2.view];
[self.window addSubview:viewController.view];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (choix==6) {
XMLTestAppDelegate *appDelegatess = (BroseFormular2*)[[UIApplication sharedApplication] delegate];
appDelegatess.viewController2.detailItem =[listOfMovies objectAtIndex:indexPath.row];
}
else if (choix==7)
{
XMLTestAppDelegate *appDelegate = (BroseFormular2*)[[UIApplication sharedApplication] delegate];
appDelegate.viewController2.detailItem =[listOfMovies objectAtIndex:indexPath.row];
}
else {
NSLog(#"no");
XMLTestAppDelegate *appDelegates = (authe*)[[UIApplication sharedApplication] delegate];
appDelegates.viewController.detailItem =[listOfMovies objectAtIndex:indexPath.row];
}
There should be only one view controller in the window on the iPhone. Only one controller should be controlling the view at a time.
This is going to be a problem:
[self.window addSubview:viewController2.view];
[self.window addSubview:viewController.view];
[EDIT] Actually, there are cases where you can have two view controllers active on the iPad, for example you can have a view controller in a pop up view.