Present UITableViewController from Storyboard - iphone

I have built up a UITableViewController inside my storyboard and now need to present it modally programatically (I can't link the transition/segue inside my storyboard).
I have linked my custom UITableViewController class to the controller I made in the storyboard and imported where required and trying to present it like so in my code:
AddEventViewController *vc = [[AddEventViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
However, it picks up nothing I have done in the storyboard, purely basing what is shown by the class contents.

Turns out I just needed to use this instead:
[self performSegueWithIdentifier:#"addEventTVC" sender:self];
Silly me.

Related

Open ViewController on button click

I am new to iOS, can any one please help me to open and activity on Button click.
I have tried below methods, but the app remains on same ViewController, i am using Singleview Application type
Second *sec = [[Second alloc] init];
[self.navigationController pushViewController:sec animated:YES];
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondV"];
[self.navigationController pushViewController:sec animated:YES];
can anyone please help me to solve this.
if you are using storyboard then Embed your viewcontroller to UINavigationController Editor->EmbedIn->NavigationController try this
If not using Storyboard, then following might help:
Second *sec = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[self.navigationController pushViewController:sec animated:YES];
For Storyboard enabled code:
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"YourIdentifierForVC"];
[self.navigationController pushViewController:sec animated:YES];
BTW, I strongly suggest you to google the problem for solution & search SatckOverflow - first, before posting as a new question.
Also, you should reconsider the naming for the class. Just a suggestion.
to push to another view controller you need to set a UINavigationController in front of your UIViewController in storyboard. Your self.navigationController might be nil, that's why it is not pushing your view controller.
Therefore drag and drop a Navigation Controller to your storyboard and delete the default TableViewController (make sure to click on empty space in storyboard before selecting and deleting it) and connect the UINavigationController to your own 'single view controller' by right-clicking on the yellow arrow and connecting the rootViewController outlet with it).
Then, select the UINavigationController on your storyboard and in Attributes Inspector (the fourth symbol in the right panel) under the section View Controller select Is Initial View Controller.

Where should I create and present a Modal ViewController used throughout iPhone application

I have an iPhone application that uses a UITabBarController for its primary interface. The application also makes heavy use of modal UINavigationControllers presented from the various tabs.
There is a ViewController that I need to present modally which can be triggered from a wide variety of locations within the application. It seems like a terrible idea to duplicate the code which creates and presents it between all the viewControllers that trigger it. I would like to have this code in a single place and trigger it from whichever viewController wants to present it.
Where should this centralised location be? My root ViewController is a UITabBarController so no good there, and I hate lumping view functionality into the AppDelegate.
I would create a new class file that has a function to present the view you want. That way you only need to write the code once to present the modal view and each view that needs to use it can call the function on the helper class with one line of code.
#interface ApplicationHelper : NSObject {
}
+(void)showMyModalView:(UIViewController *)parentViewController;
#end
Implementation:
#import "ApplicationHelper.h"
#import "ViewController.h"
#implementation ApplicationHelper
+(void)showMyModalView:(UIViewController *)parentViewController
{
ViewController *vc = [[ViewController alloc] init];
[parentViewController presentModalViewController:vc animated:YES];
}
#end
Then in each view controller import the ApplicationHelper and call the showMyModalView method
[ApplicationHelper showMyModalView:me];
This allows you to keep all the view handling code in a separate file to your application delegate.
Don't try and pass the reference around.
Just alloc] init]; a fresh instance where-ever you need to use it.
For example.
MyModalViewController *controller = [[MyModalViewController alloc] init];
[self presentModalViewController:controller animated:YES];
You can create one UIViewController and implement some delegate method for it. Then you can present the view controller like this:
[currentViewController presentViewController:yourViewController animated:YES];
And when an event is fired in your view controller, it will delegate to the caller.
In this case, you write the code only one time for "YourViewController", then reuse it where ever you want. You can also use pushviewcontroller also:
[self.navigationController pushviewcontroller:yourViewController animated:YES];

UIView presented with Black screen, fix?

I call a View to be presented with the following code:
#import "infoView.h"
...
infoView *viewInfo = [[infoView alloc] initWithNibName:nil bundle:nil];
viewInfo.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewInfo animated:YES];
But when it is presented in run-time the view that is loaded turns out black.
Currently I am using storyboard, but I need to use this code, for it is a lot more efficient in my case, because I am dealing with multiple views!
It works fine if I connect it via StoryBoard.
I should be seeing 2 labels, 1 UITextView, and 2 UIButton.
The view was created using StoryBoard, when the .m and .h files for the view where created I did not add a .xib for it. And also it is linked through the "Custom Class" section in StoryBoard.
Thanks, hope someone can help!
It's generally pretty bad form to mock people who are taking the time and effort to help you.
Naming is important it makes your code easier to work with and allows other people to use it. Not following the conventions for the language you are working in is dangerous and means that your code is not compatible with other developers as things are interpreted differently.
If you look at the docuemntation for UIViewController you'll see this note in the initWithNibName:bundle: method description
If your app uses a storyboard to define a view controller and its associated views, your app never initializes objects of that class directly. Instead, view controllers are either instantiated by the storyboard—either automatically by iOS when a segue is triggered or programmatically when your app calls the storyboard object’s instantiateViewControllerWithIdentifier: method. When instantiating a view controller from a storyboard, iOS initializes the new view controller by calling its initWithCoder: method instead. iOS automatically sets the nibName property to a nib file stored inside the storyboard.
Therefore you are instantiating your controller wrong, the storyboard should be instantiating it. Which is done like this (naming corrected)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle bundleForClass:[self class]]];
InfoViewController *infoViewController = [storyboard instantiateViewControllerWithIdentifier:#"InfoViewController"];
infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self infoViewController animated:YES];
Side note
infoView is a bad name for the class not only because you didn't start with a capital but also because it's completely deceiving. Anyone reading this would assume that InfoView is a subclass of UIView not UIViewController.

Unable to get presentViewController to work

I copied a working viewcontroller class from another project into a new project. I can't get the view to load in the new project. In the old project I used presentModalViewController. In the new I cannot get the view to load using either presentModalViewController or presentViewController
I am trying to load the present the view from my main view controller.
Here is what my main view controller interface looks like...
// ViewController.h
#import <UIKit/UIKit.h>
#import "RequestDialogViewController.h"
#interface ViewController : UIViewController <RequestDialogViewControllerDelegate> {
}
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)response;
I am using presentModalViewController like this...
RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:#"RequestDialogViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:requestIPViewController];
[self presentModalViewController:navigationController animated:YES];
and presentViewController like this...
RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:#"RequestDialogViewController" bundle:nil];
[self presentViewController:requestIPViewController animated:YES completion:nil];
What am I missing in the new project? The init method fires, but viewDidLoad does not and nothing is displayed.
Thanks
If ViewController is the root view controller, it can't present a modal view controller from within its own viewDidLoad, because at that point it doesn't have information like the screen size.
If other view controllers have already displayed, this will work. If the root view controller is a UINavigationController, you will see a view sliding in from the right while the modal view slides up from the bottom.
Anyway, for your ViewController, the soonest you could present it is after it has become visible. Using a timer for this is unreliable; older and slower devices have dramatically longer load times.
For more reliability, implement viewDidAppear: for ViewController. Do still use your timer system to add an additional delay; a fraction of a second should be sufficient. Although presenting the modal view controller from within viewDidAppear worked for me in the iOS 5.1 simulator, Presenting a modal view controller when loading another ViewController says it sometimes doesn't happen.
I have it resolved. I was trying to present the view from view did load of the main view controller. Not sure why it does not work there, but instead I am now setting a timer which calls a method to present the view controller after the main view loads and it works fine now using...
[self presentViewController:requestIPViewController animated:YES completion:nil];
Thanks to those who replied.
As #Dondragmer said, if you want to present your viewController in root view's viewDidLoad, it will fail.Once your viewController is ready for that, you can present your new viewController.
So, you can do that in
- (void)viewDidLayoutSubviews {
//present here
}
I encountered the same problem. But my situation is the presentViewController is called after the dismissViewControllerAnimated for another ViewController. My solution is to move the presentViewController to completion block of dismissViewControllerAnimated.
Present a modalViewController:
For the benefit of all starting programmers, type it instead of copy paste.
myVC *viewController = [[myVC alloc]initWithNibName:#"myVC" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
[viewController release];
It looks like you were trying to present a nav controller as a view controller in the first sample, then you were using the wrong method in the second one.

iOS - Interface Builder Outlets Not Initialized

I have created a view in Interface Builder with some labels and text as IBOutlets. I can view this screen perfectly when I segue to it from another view that I have defined in my Storyboard.
However, I have another XIB and an associated UIViewController that I want to access that view from. Because my XIB is not in the Storyboard, I cant segue to it. Instead I have to execute the transition programmatically.
PlantDetailViewController *plantDetailVC = [[PlantDetailViewController alloc] init];
[self.currentNavigationController pushViewController:plantDetailVC animated:YES];
When this code is executed it transitions to the view but the view is just blank. I have debugged the code and it enters viewDidLoad and viewWillAppear however all my IBOutlets are NIL....so nothing it showing up on screen!
Can anyone tell me why they might be NIL and how I can initialize them?
Thanks
Brian
It sounds like you're saying you have a PlantDetailViewController set up in your storyboard, and some OtherViewController that was created outside of your storyboard. Now you want OtherViewController to instantiate the PlantDetailViewController that was set up in your storyboard.
Let's say your storyboard is named MainStoryboard.storyboard.
First, you need to set the identifier of the PlantDetailViewController in your storyboard. You do this in the Attributes inspector, under the View Controller section. Let's say you set it to PlantDetail.
Then, in OtherViewController, this should work:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
PlantDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"PlantDetail"];
[self.currentNavigationController pushViewController:vc animated:YES];
-init doesn't load a nib file for you, if you want to load a nib use -initWithNibName:bundle:
If you use nib naming conventions you can pass nil to load a nib whose name matches your class and the default bundle, i.e. [[PlantDetailViewController alloc] initWithNibName:nil bundle:nil], see the docs for -nibName for details.