Xcode 6 subclasses - ios

I want to put a view controller inside a scroll view.
I think I can do it with something like:
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[scrollView addSubview:vc];
What I don't understand is how to specify my nib name correctly, because I have an interface built which I want to use but it is one of multiple views appearing in my main storyboard.

You cannot add a viewController as a subview. You can add a childViewController and the viewController's view as a subview. But you should probably read up more on how Objective-C and iOS works.

Related

Add Storyboard as subview in UIViewController

I am creating a reusable view for which I have created a separate Storyboard. This reusable view will be added in different UIViewController is Main.StoryBoard. For this I followed storyboard reference. But I am not sure how to add Storyboard as a subview inside a UIViewController having some definite frame.
Please let me know if you want to have a screenshot as to how I am designing the screen.
first you need to get the reference of the storyboard from the different storyboard.
UIViewController *viewControllerToAdd =
[[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil]instantiateViewControllerWithIdentifier:#"viewForPopover"];
//Instead of MainStoryboard you write the name of the Storyboard which you have created separately
the you write these lines
[self addChildViewController:viewControllerToAdd];
[self.view addSubview:viewControllerToAdd.view];
[viewControllerToAdd didMoveToParentViewController:self];
This much should probably do, i havent tried it yet
make reusable UIViewController and then addChildViewController wherever you want. Below i made RemarksViewController which i used in many views of UIViewController
RemarksViewController *vC=[[UIStoryboard storyboardWithName:#"yourStoryboardName" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"showRemarks"];
[self addChildViewController:vC];
vC.view.bounds=self.view.bounds;
[self.view addSubview:vC.view];
[vC didMoveToParentViewController:self];

UISplitViewController is not being displayed correctly

I created and loaded a UISplitViewController in an existing ViewController by writing the following code in the viewDidLoad method:
LeftPanelViewController *leftPanel = [[LeftPanelViewController alloc] initWithNibName:#"LeftPanelViewController" bundle:nil];
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPad" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:firstView];
UISplitViewController *splitController = [[UISplitViewController alloc] init];
splitController.viewControllers = [NSArray arrayWithObjects:leftPanel, self.navigationController, nil];
[self.view addSubview:splitController.view];
[self addChildViewController:splitController];
[splitController didMoveToParentViewController:self];
Everything is fine except for the fact that the splitController is not being drawn in the borders of the iPad, there's a space between the top of the screen and the top of the view. Even when I rotate the iPad the left panel is also having the same problem.
SplitViewController doesn't have a xib file, and when I change the added view for another that has, everything is correctly displayed.
Any ideas of what may cause this problem?
Notes:
Compiling and running the project in the simulator multiple times causes the SplitViewController to be displayed sometimes without any problems and others with spaces between any of the edges of the screen and the view. Running in the iPad causes always the same problem.
First of all.. why do you implement a container view controller? I guess you just want to present the splitViewController on its own, right? Than don't add the view yourself.
Instead correctly set it as your rootViewController on your window (preferably in applicationDidFinishLaunching).
self.window.rootViewController = splitViewController;
Container View Controller are not needed in standard cases. So you should never need to use the following methods:
addChildViewController:
removeFromParentViewController
willMoveToParentViewController:
didMoveToParentViewController:
Check the documentation of UIViewController.
If you really wanted to implement a Container View Controller, than you need to take care of the layout yourself. So you need to position / size the view of the other controller yourself. Depending on if you use AutoLayout or autoresizing, you need to set correct constraints/flags.

iOS PresentModalViewController not working

I am inside a root view controller. and then allocate the secondviewcontroller
SecondViewControl *second=[[SecondViewControl alloc] init];
[self presentModalViewController:second animated:NO];
The second viewcontroller 's view not showing up
AddSubView method works though.
[self.view addSubView:second.view];
why presetModalViewController is not working?
Usually you'll allocate the SecondViewControl with a .xib file that actually defines the user interface. Consider using a line like this (broken for readability):
SecondViewControl *second = [[SecondViewControl alloc]
initWithNibName:#"SecondView"
bundle:nil];
Without the accompanying .xib to define the view, you may be left with a view controller that doesn't have the necessary properties set to actually support user interaction, and so presentModalViewController: may have trouble there.

How to get actions from another UIViewController?

How to get actions from another controller without [self presentModalViewController: ololo animated:YES];?
Can I use just
Tutorial *ololo = [[Tutorial alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:ololo.view];
?
Right now with this code I get EXC BAD ACCESS error, when I'm trying to press button on new view.
May it will be easier to create 2 subclasses of UIView with their own XIBs, or may be I can use NavigateController without navigation bar?
PS Yes, I have Tutorial.h, Tutorial.m, Tutorial.xib. In XIB file there are 2 views (portrait and landscape).
Your question is not clear.
try to pass xibf file name as parameter to initWithNibName,ekse just use int method.hope it wont crash
Make sure that your Tutorial object extends UIViewController
#interface Tutorial : UIViewController {
Also make sure that you have a Tutorial.xib file, which has a view, and the outlet from the view it's linked with the one from the viewcontroller.
as a best practice try this :
Tutorial *ololo = [[[Tutorial alloc] initWithNibName:#"Tutorial" bundle:nil] autorelease];
[self.view addSubview:ololo.view];
Also, if you need 2 views in the same view controller, you can just add 2 views in IB, add an outlet to the second one, and add it as a subview of the main one :
[self.view addSubview:secondView]
this way, both of them are managed by the same viewcontroller, and you can add actions in the same view controller.

IOS Switch view and storyboard not works

I have a problem with switching views. I'm using a simulator with xcode 4.2
Storyboard contains:
NavigationController (initial view controller)
UIViewController which has relationship with the navigation controller
UIViewController (paired with my custom class: ViewEntryImageController) which hasn't got any relationship. Contains a button, a bottom toolbar with some toolbar button.
User come into the UIViewController, where he can see a ScrollView and in ScrollView some images.
Images has a gesture:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(openEntryImage)];
[image addGestureRecognizer:recognizer];
[recognizer release];
The openEntryImage function:
(IBAction)openEntryImage
{
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:nil bundle:nil];
controller.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];
}
When I try to tap the image, the openEntryImage works as well (the effect is correct), but I don't see my ViewEntryImageController view and my buttons, I'm only see a black window.
I try to put a NSLog line into the ViewEntryImageController viewDidLoad function, and it works, so what is the black window and where is my view controller?
When I try to use pushViewController, on the new view I found a navigation toolbar with a back button, but no other controls.
I tried another version, I created a UIViewController class, but now with a xib file. I used it instead of ViewEntryImageController and it works. Why?
I want to use this controller in storyboard too.
The ViewEntryImageController class by itself has no information about how to build the dialog. But you can instantiate your view controller on your own from the storyboard:
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:#"StoryboardFileName" bundle:nil];
ViewEntryImageController *controller = (ViewEntryImageController *)[myStoryboard instantiateViewControllerWithIdentifier:#"ViewEntryImage"];
This assumes a storyboard name of StoryboardFileName and that the view entry image controller has an identifier of ViewEntryImage set in the view properties (Attributes inspector, section "View Controller").
Try it like this :
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:#"ViewEntryImageController" bundle:nil];
If you don't use .nib names but rather use storyboards, it's a bit harder. Create a segue from the controller to the ViewEntryImageController controller by holding ctrl and dragging from one view to the other. Click this segue and give it an identifier.
Then use the [self performSegue:#"identifier"]; function to present the next view.

Resources