Problems accessing rootviewController of navcontroller iphone - iphone

Im working off the seismic xml iphone example.
In that example there is simply the application delegate which has a rootViewController of type UITableViewController.
I wanted to modify it slightly so that I would have a navigationController and use its rootViewController as the table view. My root view controller class for my navigation controller is named "firstLevelViewController"
In the sample code it says
[rootViewController.tableView reloadData];
I want to say [navController.firstLevelViewController.tableView reloadData]
but I get errors saying "request for member firstLevelViewController in something is not a structure or union"
How can I reference the rootViewController of the navigationcontroller from the main app delegate?

There isn't a firstLevelViewController property in a UINavigationController. If you want to access the current controller you can use
UITableViewController* firstLevelViewController = navController.topViewController;
assert([firstLevelViewController isKindOfClass:[UITableViewController class]]);
[firstLevelViewController.tableView reloadData];
If firstLevelViewController means the view controller at the bottom you can use
UITableViewController* firstLevelViewController = [navController.viewControllers objectAtIndex:0];
....

Access the rootViewController like this:
Objective-C
YourViewController *rootController =(YourViewController*)[[(YourAppDelegate*)
[[UIApplication sharedApplication]delegate] window] rootViewController];
Swift
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let viewController = appDelegate.window!.rootViewController as YourViewController

Related

ECSliding, referece RightController in AppDelegate

I'm using ECSliding and I have this problem!
I have a topView and two menus,
left (LeftViewController)
right (RightViewController)
both UIViewController.
I want to give a reference to the right view controller, to the left view controller, in the AppDelegate.
I did in LeftViewController.h:
#import "RightViewController.h"
#class RightViewController;
#property (strong, monatomic) RightViewController *rightView;
in didFinishLaunchingWithOptions in AppDelegate.m :
RightViewController *rightViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Right"];
LeftViewController *leftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Left"];
leftViewController.rightView = rightViewController;
but I get this error in AppDelegate.m on self.storyboard:
Property 'storyboard not found on object of type 'AppDelegate *
How can I solve this problem?
Don't statically give them a reference to each other. Instead, when they are part of the sliding view controller self.slidingViewController will be a valid reference and you can navigate based on the relationships that actually exist:
self.slidingViewController.underLeftViewController
When using it, you should check the class and cast the reference:
LeftViewController *leftController = self.slidingViewController.underLeftViewController;
if ([leftController isKindOfClass:[LeftViewController class]]) {
leftController. ...;
}
First of all AppDelegate doesn't have a property storyboard it's a UIViewController property.
Second if you want to load the main storyboard and instantiate a view controller you should try the following:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"YourStoryboardName" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"YourViewController"];
Also make sure you properly set the view controllers identifier.

How can i implement a UIModalTransitionStylePartialCurl in storyboard with Xcode 4.3?

I try this:
ViewController.h
#class SecondView;
#interface Introduccion : UIViewController{
SecondView *second;
}
-(IBAction)AnimatecreditsPage:(id)sender;
#end
ViewController.m
-(IBAction)AnimatecreditsPage:(id)sender{
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
second.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:second animated:YES];
}
Im using Storyboards, and i already linked my viewcontroller with the corresponding classes, when i press the button, the iphone simulator just crash.. im using a navigation controller and tab bar controller.
Image of the viewcontroller
THANKS!! :)
Please Help Me.
The way you're trying is crashing because you're pointing to a xib that doesn't exist. Since with storyboards you can have multiple view controllers you have to add an identifier to the view controller you wish to use in the attributes inspector section of interface builder. This then allows you to use the following code to programmatically instantiate what ever view controller in your storyboard has the ID you specify.
second = [self.storyboard instantiateViewControllerWithIdentifier:#"someID"];
Instead of:
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];

Changing properties of an open ViewController via NavigationController stack

Wondering how I can set properties of view controllers that are already on the NavigationController's stack
My situation:
I want to set up an image uploading flow like this
(Navigation Stack)
RootViewController -> TakePictureViewController -> EditPictureViewController -> UploadPictureViewController
When user confirms the upload from the UploadPictureViewController, rather than start to upload, I want to set an NSDictionary property on RootViewController which contains the upload query, then pop the navigation stack back down to the RootViewController and have it handle initiating and status reporting of the query.
Here's my code in the uploadpictureviewcontroller, currently, the code does pop to the right view controller, but the uploadPackage property is still nil, also I have tried to -setUploadPackage
RootViewController *rvc = (RootViewController *)[self.navigationController.viewControllers objectAtIndex:0];
rvc.uploadPackage = uploadPackage;
[self.navigationController popToViewController:rvc animated:YES];
All help appreciated, thanks.
try using [self.navigationController popToRootViewControllerAnimated:YES]. That should do it.
EDIT:
If you have only one instance of RootViewController, then you can set it up as a singleton and therefore you can access it from any other controller (just like the appDelegate). To do so you need to add the following to your RootViewController.m under synthesize...; :
static RootViewController *rootViewController;
+(id)sharedRootController {
return rootViewController;
}
inside your init method for RootViewController add the following line:
rootViewController = self;
now back to your UploadPictureViewController you can set the uploadPackage like this:
RootViewController *rvc = [RootViewController sharedRootController];
rvc.uploadPackage = uploadPackage;
Please note that you should NOT use the singleton method if there is to be more than one instance of RootViewController.
hope this helps!

Reference view to app delegate using storyboards

I am trying to create a UITableView using storyboard but I came to something that at the end may be easy but I have no idea how to solve it.
First of all let me point out that I know that one of the limitations of storyboards is that you will have to dig through the storyboard to find information about a view you have and link it to the app delegate.
I have create my mutable array and the information that I will use in the table in the app delegate and now I want to reference that UITableView to the app delegate. The hierarchy goes like that
First I have the root view that once you click on a button it will redirect you to the second view
Inside the second view there is another button that once you press it it will redirect you to the UINavigationController
The UINavigationController contains the UITableView.
Therefore as you can see there are two views before the navigation control and the UITableView.
Here is the code I am trying to use but it does not work
UIViewController *viewController = (UIViewController *)self.window.rootviewController;
// The next line refers to the second view but does not work at all
UIViewController *secondView = [[UIViewController viewController] objectAtIndex:1];
//Then the following line is to redirect from the second view to the navigation controller
UINavigationController *navigationController =[[secondView viewController] objectAtIndex:0];
//Then is the table view
BuildingsViewController *buildingsViewController = [[navigationController viewControllers] objectAtIndex:0];
The above code does not work. Can anyone please help me?
Thanks a lot
If this code is in the app delegate there are a variety of reasons why it will probably not work. Firstly you appear to be mixing up View's, ViewControllers and Navigation controllers with what you are trying to do. Secondly there is no guarantee at the time you are trying to do this that all of the views/viewcontrollers have yet been created yet or are joined in the way they will be when the final building view controller is rendered.
What you could try instead is in your BuildingsViewController (which you say is your table view controller) you can get a handle to the App Delegate by using
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
Once you have a handle to the delegate you can simply reference your mutable array structure etc. that you created on it from within your BuildingsViewController.
e.g. in the 'numberOfRowsInSection' method:
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
return [myBuildings count];
Or in the cellForRowAtIndexPath method:
// something like this but using your names for app delegate, building array and the accessor for the building name
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
cell.textLabel.text = [myBuildings objectAtIndex indexPath.row].theBuildingName;

how to add my view controller nib file as a sub view of another rootview controller

i am having vie controller nib file with its classes .h and .m and i want to add this view as a sub view to my rootview controller innavigation controller
how to add it..
i want a syntax to this ..
please help me....
Here is the typical code that I use to add a new view controller to my navigation controller:
NextViewController *controller = [[NextViewController alloc]
initWithNibName:#"NextViewController" bundle:nil];
[appDelegate.navController pushViewController:controller animated:YES];
[controller release];
Substitute in your own view controller class name for NextViewController above, and your own NIB file name in place of "NextViewController" above.
In my applications, the navigation controller is defined and created in my application delegate class, so appDelegate above is arrived at with the following statement:
MyAppDelegate *appDelegate;
appDelegate = [[UIApplication sharedApplication] delegate];
And you would of course substitute your app delegate class name in place of MyAppDelegate.