i am working on a navigation based application
in the rootviewcontroller i hav a few UITextFields. There is a button on rootviewController, on clicking that button i am changing the view using pushviewcontroller. I want to use values entered in these UItextFields in mapview ie my 2nd view.
PLZ suggest me something.
you could pass all the information to your second controller before pushing to navigation stack.
See the pseudo code for your reference :
First:
MyMapController* myController = [MyMapController alloc] initWithValues:Value1,value2,value3,......valuen];
[self.navigationController pushViewController:myController animated:YES];
[myController release];
myController = nil;
Second:
MyMapController* myController = [MyMapController alloc] init];
myController.value1 = value1;
myController.value2 = value2;
myController.value3 = value3;
............
myController.value7 = value7;
myController.value8 = value8;
[self.navigationController pushViewController:myController animated:YES];
[myController release];
myController = nil;
There could be more approach for sending the data,
Before pushing assign the values to the second controller.
Say you have to send value of textfield1. Now
secondController.textfield1 = textfield1;
Then do your pushing of controller.
Use an NSDictionary object to store the values from the first controller. Say you are using two text fields – textField1 and textField2.
- (void)loadSecondController {
NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:textField1.text, #"textField1", textField2.text, #"textField2", nil];
// Assuming you are using IB files
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
controller.values = values; // Declare a NSDictionary property 'values' in SecondViewController
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
Now in SecondViewController, you can access them as
...
textField1Value = [values objectForKey:#"textField1"];
textField2Value = [values objectForKey:#"textField2"];
...
Related
I used this SampleCode and created two pop-ups, say pop-up1 and pop-up2. From Pop-up1 if a button is pressed another pop-up2 will be displayed. From po-pup2 I need to go for another view. Till pop-up2 its fine, from pop-up2 I couldn't go for a separate view?
I used the below code to remove the popupview.
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
imageShareSubViewController = nil;
I used used the below code to navigate to another view.
#autoreleasepool {
ViewController *obj = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil]autorelease];
[self.navigationController pushViewController:obj animated:TRUE];
obj = nil;
}
But the above is not working fine ( i.e., i could navigate to another view ). How to solve this? Am I wrong anywhere?
create your navigationController, setRootViewController just like this:
//A view controller:
HomeScreen * homeScreen = [[HomeScreen alloc]initWithNibName:#"HomeScreen" bundle:nil];
UINavigationController* navigation = [[UINavigationController alloc]initWithRootViewController:homeScreen];
AppDelegate * delegate = (AppDelegate*)[[UIApplication sharedAplication]delegate];
delegate.window.rootviewcontroller = navigation;
[homeScreen release];
//add other views
HomeScreen * homeScreen2 = [[HomeScreen alloc]initWithNibName:#"HomeScreen" bundle:nil];
[navigation pushViewController: homeScreen2];
[homeScreen2 release];
I am using a UIButton, on clicking it I want to display the contents that are present in my NSMutableArray in UITableView with the help of UIPopOverController i.e. I want a UITableView to pop up whose cells show the contents of my NSMutableArray.
I am using the following lines of code:
UITableViewController *table= [[UITableViewController alloc]init];
table.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:table];
self.popoverController = popover;
popoverController.delegate = self;
NSString *hDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *hFilePath = [hisDir stringByAppendingPathComponent:#"hhhh.txt"];
NSArray *array = [NSArray arrayWithContentsOfFile:hFilePath ];
NSMutableArray *kkkk = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[kkkk addObjectsFromArray:[dict allKeys]];
}
table = [[UIImageView alloc] initWithFrame:[window bounds]];
// Set up the image view and add it to the view but make it hidden
[window addSubview:table];
table.hidden = YES;
[window makeKeyAndVisible];
I am unable to get the UITableView to pop up on the press of my UIButton. Can anyone help me to sort it out?
One way to show the UITableView in the UIPopOverController is by creating a new UIViewController class. And invoking it in initWithContentViewController method of UIPopOverController.
1. Create a new UIViewController or UITableViewController class. Create an instance of it.
2. Use the instance in the initWithContentViewController method of UIPopOverController.
3. Mention from where it should "pop"
For Example in your Button action :
-(IBAction)yourButtonAction:(id)sender
{
YourNewViewController*newVC=[[YourNewViewController alloc]init];
UIPopoverController*somePopOver=[[UIPopoverController alloc]initWithContentViewController:catergoryVC]; //Tell which view controller should be shown
[somePopOver setPopoverContentSize:CGSizeMake(200, 200)]; // set content size of popover
[somePopOver presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; //From where it should "pop"
}
It seems you want to present it from a UIBarButtonItem so instead of presentPopoverFromRect use presentPopoverFromBarButtonItem
[somePopOver presentPopoverFromBarButtonItem:yourBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
If you want to display popover on press of button click, then first add your button in viewcontroller, display that view controller as follows:
In app delegate write code:
MyViewController *viewController = [[MyViewController alloc] init];
[self.window addSubView:viewController.view];
In MyViewController add button and provide target to that button displayPopup as follows:
-(void)displayPopup:(id)sender
{
UITableViewController *tblViewPopover = [[UITableViewController alloc] init];
tblViewPopover.tableView.delegate = self;
tblViewPopover.tableView.dataSource = self;
tblViewPopover.tableView.backgroundColor = [UIColor whiteColor];
tblViewPopover.tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
float theWidth = 280;
tblViewPopover.contentSizeForViewInPopover = CGSizeMake(theWidth,200);
if(m_popOvrController){
[m_popOvrController dismissPopoverAnimated:NO];
[m_popOvrController release];
m_popOvrController=nil;
}
m_popOvrController = [[UIPopoverController alloc] initWithContentViewController:tblViewPopover];
[tblViewPopover release];
[m_popOvrController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
and you can use tableview delegate methods to display data in tableview.
I think, UIPopoverViewController is initialized using UIViewController and here you are using UIView(UITableView).
Can you please try using UITableViewController instead?
Also, if things does not work according to plan when you create it using the code try using an XIB explicitely.
This should help.
I have the following code:
- (void)setupBook
{
if (self.bookNavigationController) {
BookViewController *bookVC = [[BookViewController alloc] initWithTemplate:x];
self.myBookVC = bookVC;
[bookVC release];
myBookVC.pageTitle = #"My Book";
UINavigationController *nController = [[UINavigationController alloc] initWithRootViewController:myBookVC];
self.bookNavigationController = nController;
[nController release];
}
}
and then in the other parts of the code I have:
[self.someOtherNavigationController pushViewController:self.bookVC];
however now when I try to present self.bookNavigationController as a modal view controller, it is as the myBookVC is not there. why is this? It just shows up an empty view with a navigation bar.
When to first create your view controller you are assigning it to a property, however when assign to as the nav controller's rootViewController you are referencing an ivar. Use the property instead when assigning the rootViewController.
BookViewController *bookVC = [[BookViewController alloc] initWithTemplate:x];
bookVC.pageTitle = #"My Book";
self.myBookVC = bookVC;
[bookVC release];
UINavigationController *nController = [[UINavigationController alloc] initWithRootViewController:self.myBookVC];
self.bookNavigationController = nController;
[nController release];
I have a popup tableview (ViewController2) appearing when I click a button on ViewController1. Then when I select a row in the table, I want those values to be sent back to ViewController1. I have a NSDictionary set up. It works fine in a regular navigation controller, but trying to do it with dismissModalViewControllerAnimated, so the tableview drops back down, and the data is appearing in the first view.
This is similar to this question here I think: http://www.iphonedevsdk.com/forum/iphone-sdk-development/39995-return-data-dismissmodalviewcontrolleranimated.html
Here is my code:
ViewController1.h:
#protocol ViewController1Delegate;
#interface ViewController1 : UIViewController <ViewController2Delegate> {
id <ViewController1Delegate> delegate;
}
#property (nonatomic, assign) id <ViewController1Delegate> delegate;
#end
#protocol ViewController1Delegate
- (void)viewController1DidFinish:(ViewController1 *)controller;
#end
ViewController1.m:
-(void)buttonGoToViewController2 {
ViewController2 *controller = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
// this controller.delegate = self causes it to crash if i have it uncommented for some reason...
// controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}
ViewController2.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(searching) {
// Navigation logic may go here. Create and push another view controller.
NSDictionary *selectedCountry = [self.copyListOfItems objectAtIndex:indexPath.row];
ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil andDictionary: selectedCountry];
NSLog(#"selected hit this %#",selectedCountry);
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
[self dismissModalViewControllerAnimated:YES];
}
else {
// Navigation logic may go here. Create and push another view controller.
NSDictionary *dictionary = [self.listOfItems objectAtIndex:indexPath.row];
ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil andDictionary: dictionary];
NSLog(#"normal hit this %#",dictionary);
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
}
In your ViewController2 code you are creating a new ViewController1 object and giving it your NSDictionary. When you dismiss ViewController2 you go back to the original ViewController1, which never went anywhere and was never sent the NSDictionary. You need to provide ViewController2 with access to the ViewController1 object that presented it (I suggest doing it via a delegate pattern) in order to set the NSDictionary there.
hey, I'm currently using the iPhone SDK and I'm having trouble passing an NSString through 3 views
I am able to pass an NSString between 2 view controllers but I am unable to pass it through another one. My code is as follows...
`- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)index`Path {
NSString *string1 = nil;
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"items"];
string1 = [array objectAtIndex:indexPath.row];
//Initialize the detail view controller and display it.
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:[NSBundle mainBundle]];
vc2.string1 = string1;
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 release];
vc2 = nil;
}
in the "ViewController 2" implementations I am able use "string1" in the title bar by doing the following....
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = string1;
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"icon_time.png"]
style:UIBarButtonItemStylePlain
//style:UIBarButtonItemStyleBordered
target:self
action:#selector(goToThirdView)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
}
but I also have a NavBar Button on the right side that I would like to push a new view
- (void)goToThirdView
{
ViewController3 *vc3 = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:NESW animated:YES];
vc3.string1 = string1 ;
[vc3 release];
vc3 = nil;
}
How do I pass on that same string to the third view? (or fourth)
What you have should work exceptthat you want to set the string in vc3 before you push it onto the stack to ensure it is present when the view and the nav bar draws. That is the way you have it in the vc2 that works.
However, as a matter of application design, it is poor practice to pass values directly between view controllers. Ideally, you want your view controllers to be standalone and able to function regardless of which other controller did or did not precede it. (This becomes really important when you need to resume an app back to the point where it was interrupted.) If make the view controllers interdependent your app will grow tangled and complex as it grows larger.
The best way to exchange data between views is to park the data in a universally accessible place. If it is application state information put it in user defaults or you can put in an attribute of the app delegate. If it is user data, then it should go in a dedicated data model object (which is either a singleton or accessible through the app delegate.)
You may find code sample from a question I asked earlier.