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.
Related
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.
in my iPad-app I am trying to present one of my views with a modal formsheet-style.
Here's some code:
-(void)present
{
SecondViewController *modal = [[SecondViewController alloc]init];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
I am using Storyboard, and I have put stuff like a textView and toolbars in the view I'd like to show. I have set the right class in Identity Inspector, and in the class-files I have checked that it's the right view appearing with putting NSLog(#"Right view");
When calling the void present, a view is appearing, but only as a dark-white square. Nothing og my content from Storyboard is in it, I even tried changing the background color of the view and the textView to see if something was just outside the square, but the whole thing stayed white. It feels like it's not using the view I created in storyboard, but I have set it to the correct class, and the NSLog gets printed out when calling it. I have not connected the two views in any way in Storyboard, the SecondViewController is just floating around, so that might be the problem? The button that calls for -(void)present is created programmatically, so I can't ctrl+drag it to the button either.
Why is it showing an empty version of my class?
In the "Identity Inspector" set a "Storyboard ID" for your ViewController, and then present it like this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
And if you're using iOS6, presentModalViewController:animated: is deprecated, so use this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentViewController:modal animated:YES completion:nil];
}
Your problem is that you're assuming the program will intrinsically know where to find the, already laid out, view for this controller when that's simply not how storyboards work. The code you list about will create a view controller, but without an associated view it will simply show as a black square.
There's a few ways to solve your dilemma:
Add the modal transition as a segue in the view controller, this would be the simplest way and is what iOS storyboards expect you to do.
Move the view from the storyboard to an external .xib and call the initWithNibName:bundle: method to load this as your view controller's view. This is the best solution if you just want to programmatically load the view.
Load the view from your storyboard programmatically with the instantiateViewControllerWithIdentifier: method, this is probably a bad idea as it goes against the design of storyboards.
I can elaborate on those if you want.
Hy
i have two classes uiviewcontroller and uiview. I have one view controller. Inside i have uiview. Inside uiview i have textfield and when i write a text and click done i need to refresh uiviewcontroller.
I tried with this in uiview class:
-(IBAction)textFieldReturn:(id)sender
{
ViewController *vc = [[ViewController alloc] init];
[vc viewDidLoad];
}
i need refresh the same as you click the button and open viewcontroller.
I am guessing you mean that you want to "refresh" the view, not the view controller. To do that simply call [self setNeedsDisplay] from the view, or [self.view setNeedsDisplay] from the view controller. Also make sure that the textfield is a subview of the uiview. Either do that in the nib file or in code by calling [self addSubview: (textfield here)].
Also, if you want to access the view controller from the view you will need to create an IBOutlet, simply allocating a new ViewController object within the view does not mean that the created view controller controls the view. Hopefully that makes sense. I'd recommend going through some ios starter tutorials as well. Just google that there are a lot.
I have a view controller embeded in another view using 'addSubView' that is not catching events, how can I make sure it does?
background:
I'm attempting to nest views in order to break up a storyboard that is to be shared by multiple devs. To accomplish this with minimal duplication of functionality I/we have created a mainStoryboard which contains a tab controller and 4 tabs, each tab contains a subview that loads a UIView (contained in another storyboard) into itself. These views are added like so:
//Add sub view
UIStoryboard *board = [UIStoryboard storyboardWithName:#"MessagesStory" bundle:nil];
UIViewController *boardController = [board instantiateInitialViewController];
[self.view addSubview:boardController.view];
boardController.view.frame = CGRectMake(0, 0, 320, 480);
The initial view controller that is loaded is a UITableView subclass, the whole thing works great for rendering the table and it's contents to the screen and I can interact with the table and select rows, however the event listener on the the views controller 'didSelectRowAtIndexPath' fails to fire.
I know it's not firing thanks to good ol' NSLog():
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Please kind sirs help a poor string appear in the console.");
}
I know it is related to the subview because if I load the subview on it's own as the main view the event listener functions properly.
Any help is appreciated as this is taking longer than expected and I may be pulled to another project before I can finish implementing this.
I figured it out.
It was stunningly simple but I had to do quite a bit of reading to find the answer. All my searches where for 'addSubView' tutorials and examples I didn't even know 'addChildViewController' existed.
Anyway I believe it's this simple:
-(void)viewDidAppear:(BOOL)animated
{
if (firstLaunch) {
firstLaunch = NO;
//Find the view controller in the other storyboard
UIStoryboard *board = [UIStoryboard storyboardWithName:#"MessagesStory" bundle:nil];
UIViewController *boardController = [board instantiateInitialViewController];
//add it as a child view controller (THIS IS WHAT I WAS MISSING)
[self addChildViewController:boardController];
//now it is okay to add the subview
[self.view addSubview:boardController.view];
//trigger this method (also missing this but it will run without it, I assume is good practice)
[boardController didMoveToParentViewController:self];
}
[super viewDidAppear:animated];
}
Once I knew I wanted to 'addChildViewController' it was easy to find information:
How does View Controller Containment work in iOS 5?
Is it wise to "nest" UIViewControllers inside other UIViewControllers like you would UIViews?
This is the first time I'm trying to implement navigation from a tableView cell to another tableView using UINavigationController and it doesn't work for me.
I'm NOT using nib file and I have a simple tableView that I present it in a modal dialog in my app, it works fine, now I added the disclosureInidcator to one of it's cell, to make the user enable to choose from a fixed number of options available from another list(tableView). For this purpose I have another class that makes the second tableView. the problem is now navigation from the cell(contains disclosure icon)in first tableview to second tableView doesn't do anything, no error, no nothing. I guess the way I setup the navigation controller would be wrong, the code doesn't fall in delegate, or datasource of the second class at all.
in First TableView in method : didSelectRowAtIndexPath I tried to catch that row, then call the second tableView like this:
mySecondViewController *secondVC = [[[mySecondViewController alloc] initWithStyle:UITableViewStyleGrouped ] autorelease];
UINavigationController *navCont = [[UINavigationController alloc] initWithRootViewController: self];//not sure the first controller should act as the root controller?
[navCont pushViewController:secondVC animated:YES]; //it does nothing, no error,...
the second tableViewcontroller class contains all delegate and datasource methods, and initialization method:
- (id)initWithStyle:(UITableViewStyle)style
{
if ((self = [super initWithStyle:style])) {
}
return self;
}
and declared in interface as:
#interface stockOptionViewController : UITableViewController {
}
I tried to play with viewDidLoad, but didn't help.
Please help me cause I have no clue and all sample codes found is based on using nib files.
Thank,
Kam
Your navigation controller should be the root view controller of the app delegate's window, and the first view controller should be the root view controller of the navigation controller, then you can push new controllers onto it.
Please see the documentation for UINavigationController.
At the moment, you are creating a navigation controller but not putting it anywhere, so asking it to push new view controllers is a little pointless. You have the right code, just not in the right order.
You can present view control modally without nav controller
mySecondViewController *secondVC = [[[mySecondViewController alloc] initWithStyle:UITableViewStyleGrouped ] autorelease];
[self presentModalViewController:secondVC animated:YES];
UINavigationController should be the root view controller. In the current code, navCont is not on the view stack, so it won't work. Instead of pushing myFirstViewController in your appDelegate, push the UINavigationController to the stack and add myFirstViewController as its root view controller.