passing BOOL when back button pressed - iphone

I customized the action of back button. I want to send to parent view a BOOL if back is pressed, but the bool value is always null.
my parent .h
[...skip...]
BOOL myBool;
[...skip....]
my parent .m
#import "theChild.h"
....
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"myBool is %d", (int)myBool);
}
-(IBAction)callTheChild:(id)sender {
theChild *theChildVC = [[theChild alloc] initWithNibName:#"theChild" bundle:nil];
// set something
[self.navigationController pushViewController:theChildVC animated:YES];
[theChildVC release];
}
in my theChild .m
#import "theParent.h"
....
....
-(void)backAction:(id)sender {
theParent *theParentVC = [[addSite alloc] init];
// set parent BOOL
theParentVC.myBool = YES;
[addVC release];
// dismiss child view
[self.navigationController popViewControllerAnimated:YES];
}
when the parent appear, myBool is null.
if I change
[self.navigationController popViewControllerAnimated:YES];
to
[self.navigationController pushViewController:theParentVC animated:YES];
all works fine but is not what I want for several reasons.
Any help is appreciated.
Thanks,
Max

You're not passing the bool back to the parent, you're creating a completely new object and giving that the bool instead!
Look at this line :
theParent *theParentVC = [[addSite alloc] init];
That line has made a new parent object. You probably wanted to use the original parent object :)
in theChild.h
[snip]
theParentVC *parent;
[snip]
when you create the child
-(IBAction)callTheChild:(id)sender {
theChild *theChildVC = [[theChild alloc] initWithNibName:#"theChild" bundle:nil];
[theChild setParent:self];
[self.navigationController pushViewController:theChildVC animated:YES];
[theChildVC release];
}
and when you want to update the parent
-(void)backAction:(id)sender {
// Update the parent
parent.myBool = YES;
// dismiss child view
[self.navigationController popViewControllerAnimated:YES];
}

You are creating a new viewcontroller, not linking back to the true parent.
try
self.parentViewController.myBool = YES;
rather than
theParent *theParentVC = [[addSite alloc] init];
// set parent BOOL
theParentVC.myBool = YES;

Related

navigate from one page to another page onclick

I would like to know a method for navigating from one page to another page onclick .I
have tried
[self.navigationController
pushViewController:self
.objectNewlist
animated:YES];
but it is showing the error
Incompatible pointer types sending 'newlistplist *' to parameter of type 'UIViewController *'
my main class file is a subclass of UIViewController
and the second class is a subclass of UITableViewController
can any one help me out with this problem?
You can use
UIViewController *yourViewController = [[YourViewControllerClass alloc] initWithNibName:#"<name of xib>" bundle:nil];
[self presentModalViewController:yourViewController animated:YES];
[yourViewController release];
In case the new view is also to be created programmatically, you can do that in the viewDidLoad method of YourViewControllerClass and change the initialization to
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
In YourViewController when you wish to come back to previous view on some button action you can use
[self dismissModalViewControllerAnimated:YES];
Another way that you can do is
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
[self addSubview:[yourViewController view]];
and to remove the view you can use
[self.view removeFromSuperview];
-(void)onClickButton
{
ViewControllerClass *objViewControllerClass = [ViewControllerClass alloc] init] ;
[self presentModalViewController:objViewControllerClass animated:YES];
[objViewControllerClass release];
}
objViewControllerClass (object of another class)

Two UIViews for one ViewController

I'm not sure how to do this. So I originally had a ViewController that had one .xib, with one main view. I present it like this:
DogViewController *dvc = [[DogViewController alloc] initWithNibName:#"DogViewController" bundle:nil];
dvc.modalPresentationStyle = UIModalPresentationFormSheet;
dvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:dvc animated:YES];
[dvc release];
So that works fine. However now from a button press in the DogViewController.xib, I want to dismiss the current form sheet, and show another form sheet with some additional questions before proceeding. So I started by adding another view to in my original .xib of DogViewController, then got stuck in the logic of how to dismiss the first one, and show the second one. I'm assuming I need some outlet to the new view in the same .xib, but from there I'm lost. Thanks.
The way to do this would be to set it up with a UINavigationController as Mathiew mentions. However, if you really want to transition between two views on one view controller, you can refer to this sample code from Apple:
http://developer.apple.com/library/ios/#samplecode/ViewTransitions/Introduction/Intro.html
The code uses ImageViews to demonstrate the effect but I don't see why you can't use views instead :)
You can add a view within the other view in front of all of the other objects and just use its hidden property to control whether it's shown or not.
Why don't you use a navigation controller in your modal view, create another xib and do a [self.navigationController pushViewController:secondViewController animated:YES];
If you have a good reason, you can set a second view outlet secondView and use code like
UIView* superview = [self.view superview];
[self.view removeFromSuperView];
[superview addSubview:self.secondView];
Very simple solution is to hold reference to MainViewController and call methods on it that swap between two view controllers.
Like this:
#implementation MainViewController
- (void)showDogViewController {
[self dismissModalViewControllerAnimated:YES];
DogViewController *dvc = [[DogViewController alloc] initWithNibName:#"DogViewController" bundle:nil];
dvc.modalPresentationStyle = UIModalPresentationFormSheet;
dvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
dvc.mainViewController = self;
[self presentModalViewController:dvc animated:YES];
[dvc release];
}
- (void)showCatViewController {
[self dismissModalViewControllerAnimated:YES];
CatViewController *cvc = [[CatViewController alloc] initWithNibName:#"CatViewController" bundle:nil];
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.mainViewController = self;
[self presentModalViewController:cvc animated:YES];
[dvc release];
}
}
#end
#implementation DogViewController
- (void)showCatViewController {
[mainViewController showCatViewController]
}
#end
#implementation CatViewController
- (void)showDogViewController {
[mainViewController showDogViewController]
}
#end

data passing in navigation based application

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"];
...

How can I pass a parameter into a view in iOS?

UIViewController *theController = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];
Here's my code for showing my view. I know I can use app delegate variables, but it would be neater is I could pass a parameter in somehow, ideally using an enum. Is this possible?
Just create a new init method for your HelpViewController and then call its super init method from there...
In HelpViewController.h
typedef enum
{
PAGE1,
PAGE2,
PAGE3
} HelpPage;
#interface HelpViewController
{
HelpPage helpPage;
// ... other ivars
}
// ... other functions and properties
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle onPage:(HelpPage)page;
#end
In HelpViewController.m
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle onPage:(HelpPage)page
{
self = [super initWithNibName:nibName bundle:nibBundle];
if(self == nil)
{
return nil;
}
// Initialise help page
helpPage = page;
// ... and/or do other things that depend on the value of page
return self;
}
And to call it:
UIViewController *theController = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil onPage:PAGE1];
[self.navigationController presentModalViewController:theController animated:YES];
[theController release];
I generally just have certain variables in my UIView, which I set from the parent view. To pass variables back, I make use of the function:
[[[self.navigationController viewControllers] lastObject] setFoo:foo];
Define a setter for the parameter in HelpViewController and change your code to:
HelpViewController *theController = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[theController setSomeValue:#"fooBar"];
[self.navigationController presentModalViewController:theController animated:YES];

Create a new ViewController object only if it doesnt exist

I use the following code to bring up a view
-(IBAction) openSomeView{
SomeView *sv = [[SomeView alloc]initWithNibName:#"SomeView" bundle:nil];
[self presentModalViewController:sv animated:NO];
[sv release];
}
How can I detect if this view has been created already and if so, then just show it now create a new object?
Thanks
first declare in #interface
SomeView *sv;
and the you can check it
-(IBAction) openSomeView{
if(sv==nil)
sv = [[SomeView alloc]initWithNibName:#"SomeView" bundle:nil];
[self presentModalViewController:sv animated:NO];
[sv release];
}
You cannot create more than one instance using this code, since you present modally the view controller.
Else, you'd probably keep a member variable in your class and check it against nil.
EDIT: or you can implement the 'Singleton' design pattern, if that's the meaning you search.
I have a couple of heavy-weight view controllers in my app. Right now I'm handling this situation as follows:
save MyViewController to appDelegate
in "openSomeView" method I'm checking if MyViewController was already created, if no - create it:
-(IBAction) openSomeView {
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if ( !appDelegate.myViewController ) {
appDelegate.myViewController = [[SomeViewController alloc] initWithNibName:#"SomeViewController"
bundle:nil];
}
[self presentModalViewController:appDelegate.myViewController animated:NO];
}