I have two view controllers name RootViewController and SecondViewController. In the FirstViewController I have this NSMutableArray
#interface RootViewController : UITableViewController {
NSMutableArray *allClasses;}#property (nonatomic,retain) NSMutableArray *allClasses;
In the RootViewController I populate the UITableView with all the objects within allClasses
In my SecondViewController I have
#interface SecondViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate> {
NSMutableArray *arrayStrings;}
I have a method that adds new NSStrings to the arrayStrings. My goal is to be able to pass the arrayStrings to the RootViewController by trying something similar to allClasses = arrayStrings. That way when the RootViewController is loaded it can populate with new information.
How would I got about accomplishing that task?
in RootViewController implement
-(void)viewWillAppear:(BOOL)animated
this delegate method. Inside this method
write
self.allClasses = SecondViewControllerObj.arrayStrings;
SecondViewControllerObj is the object of SecondViewController declared in RootViewController as a member and which is used to navigate to that view
like
if(SecondViewControllerObj == nil)
{
SecondViewControllerObj = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}
[self.navigationController pushViewController:SecondViewControllerObj animated:YES];
You need to have the reference of root view controller in the second view controller. And there you need to set the allclasses to arrayStrings;
In general you can find reference to any view controller pushed into the stack by the following code.
NSArray *viewConts = [[self navigationController] viewControllers];
for(int i=0;i<[viewConts count];i++)
{
if([[viewConts objectAtIndex:i] isKindOfClass:[RootViewController class]]){
RootViewController *rootController = (RootViewController *)[viewConts objectAtIndex:i];
[rootController setAllClasses:arrayStrings];
}
}
Now in the viewWillAppear of RootViewController you need to reload the contents of your view.
Hope this helps.
Thanks,
Madhup
Related
I have a storyboard based Single View App;
I have 3 ViewControllers on my Storyboard linked to 3 ViewController classes in the code;
I browse between ViewControllers by doing this:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
MenuViewController* mainMenu = [sb instantiateViewControllerWithIdentifier:#"vcMainMenu"];
mainMenu.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:mainMenu animated:YES];
Now I need to access different UIcontrols on the applicationWillResignActive from the current active ViewController, I will access different controls depending of the ViewController, I'm trying to accomplish this by doing:
if ([self.window.rootViewController isKindOfClass:[LowProfileViewController class]])
{
NSLog(#"here!");
}
But it always returns the rootViewController. How Can I get the current displayed rootViewController from applicationWillResignActive?
Please, Incorporate NavigationController is not an option...
thanks
You could try this -
Make An AppDelegate #property (i.e. currentModelViewController)
#property (nonatomic, retain) UIViewController *currentModelViewController;
For each ViewControllers in viewDidAppear or in viewWillAppear
appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.currentModelViewController = self;
Now this will work
if ([self.currentModelViewController isKindOfClass:[LowProfileViewController class]])
{
NSLog(#"here!");
}
I have a VC that presents another VC modally. This 2nd VC pushes to a 3rd VC. The 3rd VC dismisses itself and needs to call a method on the initial VC. I can't seem to get this working. Any advice?
VC3 *aVC3 = [[self.navigationController.viewControllers objectAtIndex:0] parentViewController];
aVC3 = self.name;
[aVC3 addExercise];
[self dismissView];
Use delegate for backward messsaging.
Refer simple-delegate-tutorial-for-ios-development. Also refer the-basics-of-protocols-and-delegates
If you have UINavigationController there is no need of delegate to communicate to any viewController in navigationController like:
ViewController *objViewController = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0]; //need to which controller u would like to communicate
[objViewController yourMethodHere]; // your method here
Here is an alternate method:
Step 1: Create a FirstViewController Property in the SecondViewController.h
#interface SecondaryViewController : UIViewController{
}
#property (nonatomic, strong) FirstViewController *viewControllerOne;
Step 2: set that property to self when presenting the SecondViewController in FirstViewController.m
#implementation SecondaryViewController
-(void)functionThatPresentsSecondViewController{
SecondViewController *viewControllerTwo = [[SecondViewController alloc] init];
[viewControllerTwo setViewControllerOne: self];
[self presentViewController:viewControllerTwo animated:NO completion:nil];
}
Step 3: You can now call FirstViewController functions from SecondViewController by using the viewControllerTwo.viewControllerOne property.
I need to get an NSString from my parent view controller to its child view.
So I have 'ParentView' ----> 'ChildView'
And I need to get the string from ParentView to ChildView. I have tried adding a method which returns a string in my ParentView and calling it like so in the ChildView with no luck.
Doesn't work:
ViewController *vc = [[ViewController alloc] init];
startDateLbl.text = [vc string];
Any help as to how to achieve this would be appreciated, thanks.
The easiest way would be to set a property for your string in the child view.
#property (nonatomic, retain) NSString *childString;
Then pass your string from the parent view to the child view before or after you push the view onto the stack.
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController"];
vc.childString = parentString;
[self.navigationController pushViewController:vc animated:YES]; // this assumes navController
This line ViewController *vc = [[ViewController alloc] init]; will create a new object.
Just giving you a simple code hope you get it
ChildViewController *viewController = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
//here set your label, make sure startDateLbl has getter property
viewController.startDateLbl.text = [vc string];
Depending on the UIViewController type you may have access to the parent View Controller.
self.parentViewController.someString (see UIViewController documentation).
I have a root view controller which should load another view controller as soon as it is done loading (i.e. in the viewDidLoad method).
I am using the UINavigationController in order to push a new view controller onto the stack:
In my rootviewcontrollerappdelegate:
-(void) viewDidLoad{
LoginViewController* lvc = [[LoginViewController alloc]init];
[self.navigationController pushViewController:lvc animated:NO];
}
I have textfields and buttons in the view controller to be loaded. The above doesn't seem to work however...It loads just a blank grey screen and no UINavigation bar is present. If I comment out the second line (pushViewController line), then I see the navigation bar. So I think it is loading something, but the items in the view controller being loaded are not being shown...Any ideas why?
Check if navigationController is pointing to nil. If it does, try
[self.view addSubview:self.pushViewController.view]
I had the same problem and found the above solution here:
UIViewController -viewDidLoad not being called
Unless you're doing something tricky, you should be calling alloc on the LoginViewController class rather than a variable. Also, if you've set up LoginViewController in Interface Builder (as opposed to programmatically), you'll need to load it from an NIB:
LoginViewController *lvc = [[[LoginViewController alloc] initWithNibName:nil bundle:nil] autorelease];
[self.navigationController pushViewController:lvc animated:NO];
Have a look at initWithNibName:bundle: in the docs.
Not entirely sure what you are trying to achieve but when you instantiate LoginViewContoller it should probably look like this
LoginViewController* lvc = [[LoginViewController alloc]init];
Judging by the nature of your naming for your view controller, is your LoginViewController the first view controller for your UINavigationController?
If that is what you're trying to do, you should instead initialise your navigation controller with the LoginViewController as the root controller instead of pushing it onto the navigation stack.
UINavigationController has a method to do this:
- (id)initWithRootViewController:(UIViewController *)rootViewController
EDIT:
Well, one way you can go about it is like this.
In your application delegate .h file, you should have declared a UINavigationController.
#interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
UINavigationController *navController;
}
#property (nonatomic, retain) UINavigationController *navController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
In your App Delegate didFinishLaunching:withOption: you can create an instance of your LoginViewController there, and use that to init your UINavigation controller as the root view controller
#import "LoginViewController.h"
#implementation MyAppDelegate
#synthesize navController;
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginViewController *loginController = [[LoginViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:loginController];
[loginController release];
[[self window] setRootViewController:navController];
[navController release];
[self.window makeKeyAndVisible];
return YES;
}
I probably have a typo here or there but that's one way I would go about doing it.
I have a custom customUIViewController in a UISplitViewController and want to access the instance of the customUiViewController from the detailView (which is another UIViewController inside the UISplitViewController) from another class; how can I do this?
CODE SNIP (Dont worry about the syntax; it is shorten up)
myAppDelegate.m
customViewController *masterView = [[customViewController alloc] init;
UINavigationController *NVC = [[UINavigationController alloc] initWithRootViewController:masterView];
MYViewController *detailView = [[MyViewController alloc] init;
UISplitViewController *mySplit = [...];
mySplit.viewControllers = NSArray[...masterview,detailView,nil];
[window addSubView:mySplit view];
MyViewController.m
-(void) someMethod {
customViewController *myInstance = (customViewController)[self.splitViewController objectAtIndex:0]; ??
// I think this just gets the outter UINavigationController
[myInstance doSomething];
}
customViewController.m
-(void) doSomething {
}
I want to be able to get access to customViewController to call the doSomething method. Both customViewController and myViewController is inside the same UISplitViewController
UIViewControllers have a splitViewController property so try using that to get a reference:
customViewController *myInstance =
(customViewController *)[self.splitViewController.viewControllers
objectAtIndex:0];
Index 0 is the left-side view controller in the split view controller.
Edit:
If the left-side view controller is a UINavigationController, then to get the root view controller of that, do this:
UINavigationController *nc =
(UINavigationController *)[self.splitViewController.viewControllers
objectAtIndex:0];
customViewController *myInstance =
(customViewController *)[nc.viewControllers objectAtIndex:0];
If you're working with the default UISplitView that XCode makes, you need to reference the AppDelegate to get the splitView's ivar:
YourAppDelegate *del = (YourAppDelegate *)[[UIApplication sharedApplication]delegate];
UISplitViewController *split = del.splitViewController;
NSArray *vcArray = split.viewControllers;
//left is objectAtIndex:0, right is objectAtIndex:1