How to navigate a view to subview in a buttonclick? - iphone

Ha ii.I have added a subview by using interface of the main view,and i insert some controls in the subview,i add outlet for this subview,the problem is i want to navigate this subview from the main-view by button click.
How to do this?
Thanks in advance.

To navigate from subview in a navigation based application...(subView1 is an instance of your suubview)
[self.navigationController pushViewController:subview1 animated:TRUE];

Import your second class in .m file then,
In the method of button click event write following code, where ClassName is the name of second class in which you want to navigate.
ClassName *cls = [[ClassName alloc]initWithNibName:#"ClassName" bundle:nil];
[self.navigationController cls animated:NO];
[cls release];

Related

loading viewDidLoad at second xib issue

I have two xib files. I start with "ViewController.xib" and when the user clicks a button it loads a different xib file named "GamePage.xib".
The code of the button is:
-(IBAction)startButtonClicked{
UIViewController *gamePage = [[UIViewController alloc] initWithNibName:#"GamePage" bundle:nil];
[self presentViewController:gamePage animated:NO completion:nil];
}
When the button is clicked, the second xib is shown on screen, but its "viewDidLoad" method don't run…
Why is that?
UIViewController *gamePage = [[UIViewController alloc] initWithNibName:#"GamePage" bundle:nil];
This does not seem correct.
viewDidLoad is probably running but it is viewDidLoad of Apple's UIViewController. You should use your class instead of UIViewController when you initialize it. In general you should use the class which viewDidLoad you need called.
Your XIB must have its Owner. You need to create a class derived from UIViewController and then you need to make that class owner of your XIB.
Just follow these steps:
Open you project
Select the Project Name in the Left Navigation Panel
Right Click and select New File
Select Objective C Type Class
When you click on it, it will ask you to give it a name
Suppose you gave GameViewController
Check the "With XIB for Interface"
and then add the files
Now in your Navigation Panel you will see three classes
GameViewController.h
GameViewController.m
GameViewController.xib
Create your interfaces in your xib.
And now if you have to move to this class and show its view, write the following code:
-(IBAction)startButtonClicked{
GameViewController *gamePage = [[GameViewController alloc] initWithNibName:#"GameViewController" bundle:nil];
[[self navigationController] pushVIewController:gamePage animated:YES];
[gamePage release]; //If not used ARC
}

How to use PushViewController on UIViewController?

I have an UIViewController in that I have a navigation bar and a table, now I want to navigate from this view to another view on selection of the table row.
How can I do that?
First of all add the new UIViewController to your application by right clicking in files and groups section and Clicking "New File" option and name it SecondViewController.
Make sure you add an XIB by selecting "With XIB for user interface" option while creating the SecondViewController
Suppose you want to push the new view on button click then add a button to you FirstViewController and add the following code on its button's TouchUpInside event:
SecondViewController *secondView=[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondView animated:YES];
[secondView release];
if you are using ARC then remove [secondView release];.
Let me know if you need more help.
Hope this helps.
FileName *file=[[FileName alloc] initWithNibName:#"FileName.xib" bundle:nil];
[self.navigationController pushViewController:file animated:YES];
[file release];

UIBarButtonItem not reacting to click [duplicate]

From the rootViewController I navigate to a UIViewController
if (self.contr == nil) {
ExampleViewController *controller = [[ExampleViewController alloc]
initWithNibName:#"Example"
bundle:[NSBundle mainBundle]];
self.contr = controller;
[controller release];
}
[self.navigationController presentModalViewController:self.contr animated:YES];
In the UIViewController I have the method
-(IBAction) goBack:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
I added the signature to the .h file.
In the .xib file, I have a UIToolbar with a UIBarButtonItem. I connected the button to the File's Owner - goBack:
Everything appears in the screen, but when I click on the button, goBack isn't called. I also tried to do this programatically instead, but I got the same result - everything appears, but no reaction to the click.
Any ideas why it isn't working?
Edit:
I just found out something invisible is over the toolbar. If I click on a specific point (over the toolbar), then goBack: is called. Since I navigated to this screen using presentModelViewController, the navigation bar isn't appearing... but probably it's there and that's what is hiding the tool bar.
Have bind your Toolbar with File Owner?
As your UIBarButton is subview of UIToolbar so you have to bind Toolbar with File Owner.
Presenting a modal view controller do not require you to pass through a UINavigationController. I suggest you to change this:
[self.navigationController presentModalViewController:self.contr animated:YES];
[self.navigationController dismissModalViewControllerAnimated:YES];
to this:
[self presentModalViewController:self.contr animated:YES];
[self dismissModalViewControllerAnimated:YES];
Let me know if this helps.
Try this in the goBack method :
[self.navigationController popToRootViewControllerAnimated:YES];
If you are not hitting the breakpoint that means you did not connect them properly in the xib.

How to change/add view to rootcontroller's view in UINavigationController

I have an application where I need to have a "custom" setting page. In my delegate I add a UINavigationController's view to the window with a UIViewController as rootviewcontroller.
In the rootviewcontroller I want to have a button and when I press the button the whole view changes to the settingview that I made. I only need the code to change view.
Thanks for the help in advance.
If Settings View a UIView SubClass then In the Button Action method you can do something like this:
[self.view addChild: settingsView]; //provided that settingsView is already allocated.
If you have written down a separate UIViewController SubClass for Settings then you can do something like this in your Button Action Method:
SettingsViewController *controller=[[SettingsViewController alloc]initWithNibName:#"SettingsViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

Reload a table view on close of add subview

I have three objects in the applicaion. There is a UItableviewcontroller(with nib file) that shows a list of the items. One UIviewcontroller to add item (with nib file) and a model class that contains item object.
I show the list of item firstly (on application start). I have a navigation button on navigation bar to pop up add view (add as subview) on the same screen (on table view). In add view I have a add button. when I click on add button it adds the record and disappear from the table view but doesn't reload the that.
I have used following code in add item button click action
listitem *home= [[listitem alloc] initWithNibName:#"listitem" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
[home viewWillAppear:YES];
[home release];
[self.view removeFromSuperview];
In viewwillappear function I am reloading the data from database and also reloading the table view data using reloadData.
Am I doing correct. What is the mistake I am doing.
Your code is very tricky to read* but this is what I think you're doing:
You're making a new list each time that you add an item. you don't want to create a new home object, you want to go back to the last one?
i.e. replace your code with
[self.navigationController popViewControllerAnimated:YES];
and this will go back to your original list, which should have refreshed itself (a UINavigationController will call viewWillAppear for you).
Hope that helps.
NB You have to have used a navigation controller to add your 'add item' view otherwise this code won't work :( This is how you should be adding your item view.
AddItem *home = [[AddItem alloc] initWithNibName:#"AddItem" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
This will slide on your add item view.
If you want the add item view to be a popup, a UINavigationController is definitely not the way to do it!
You will need to tell your initial list view when it needs to update itself. You can do this using either a delegate or a notification. I'd go for a delegate in this case.
You need to add this to your add item view controller's code (AddItem's .h file)
#interface AddItem : UIViewController {
UITableViewController *delegate;
}
#property (nonatomic, assign) UITableViewController *delegate;
#end
and synthesize it in your AddItem's .m file
#synthesize delegate;
When you create your add item view controller, set the delegate to be the initial view controller. i.e.
AddItem *home= [[AddItem alloc] initWithNibName:#"AddItem" bundle:nil];
home.delegate = self;
home.view.frame = CGRectMake(10, 20, 300, 300);
[self.view addSubview:home.view];
Finally, when you have added a new item, tell your delegate to refresh itself like so :
[delegate reloadData];
[self.view removeFromSuperview];
*It's standard practice to use capital letters for class names i.e. listitem should be called ListItem. Personally, I'd call it ItemListViewController so it's clear what it does.