How to pass value to another view-controllers? - iphone

I want to pass localstringtextnote to Uploadviewcontroller by this way ,
UIViewController *controllerNew = [[UploadViewController alloc] initWithNibName:#"UploadView" bundle:nil owner:self];
controllerNew.localStringtextnote = localStringtextnote;
[self.navigationController pushViewController:controllerNew animated:YES];
[controllerNew release];
but i got this error"#property localstringtextnote not fond in the object of type uiviewcontroller"
or
i want to pass through modalTransistionstyle
UploadViewController *aSecondViewController = [[UploadViewController alloc] initWithNibName:#"UploadView" bundle:nil];
aSecondViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:aSecondViewController animated:YES];
[UIView commitAnimations];
How to do this?Thanks in advance.

Declare the variable
NSString *_localStringtextnote;
in UploadViewController.h file.
Also add a property for that variable in the same file.
#property(nonatomic,copy) NSString *localStringtextnote;
In .m file, synthesize that inside the implementation.
#synthesize localStringtextnote=_localStringtextnote;
Now run the app.

UploadViewController *controllerNew = [[UploadViewController alloc] initWithNibName:#"UploadView" bundle:nil owner:self];
controllerNew.localStringtextnote = localStringtextnote;
[self.navigationController pushViewController:controllerNew animated:YES];
[controllerNew release];
change your uiview controller to UploadViewController as uiview controller doesn't have any property localStringtextnote

You have to declare localStringtextnote as the property in the UploadViewController as #property(nonatomic,retain)NSString * localStringtextnote; and synthesize in .m file.

create a new method in controllerNew (don't forget to declare it in *.h file):
-(id) setParams:(NSString*)str {
localStringtextnote = [[NSString alloc] initWithString:str];
return self;
}
Then, when you need to puth this view controller:
UploadViewController *controllerNew = [[UploadViewController alloc] initWithNibName:#"UploadView" bundle:nil owner:self];
[self.navigationController pushViewController:controllerNew animated:YES];
controllerNew = [controllerNew setParams:localStringtextnote];
[controllerNew release];

Related

pushViewController doesnt work even though i have a navigationController

I have a UIViewController with two UITableView's in it. When i select a row in the first UITableView it has to push the same UIViewController which doesnt happen.
In UIViewController.h i have,
FirstTableViewController *firstController;
SecondTableViewController *secondController;
In UIViewController.m i have,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if (firstController == nil) {
firstController = [[FirstTableViewController alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTableViewController alloc] init];
}
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstController];
firstController.product = self.product;
[firstTable setDataSource:firstController];
[firstTable setDelegate:firstController];
firstNavigationController.view = firstController.tableView;
}
In FirstTableViewController.m, didSelectRowAtIndexPath i have,
[self.searchDisplayController setActive:YES animated:YES];
UIViewController *controller = [[UIViewController alloc]initWithNibName:#"UIViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.product = prod;
NSLog(#"Navigation controller is %#",self.navigationController); //not null
[[self navigationController]pushViewController:controller animated:YES];
Please help.
EDIT #1: UIViewController is called from the FlipsideViewController of the UtilityApp.
Add a property to your AppDelegate UINavigationController *nav
add this line to AppDelegate.m in application didFinishLaunching method
navigationControl = [[UINavigationController alloc] initWithRootViewController:yourFirst ViewController];
Go to yourFirstViewController, add an UINavigationController *nav property and add these lines to viewDidLoad method
AppDelegate *app = [[UIApplication sharedApplication] delegate];
self.nav = app.nav;
Use this line to push any viewController
[self.nav pushViewController:controller animated:YES];
If you want to show a view controller modally, you should use presentViewController:animated:completion: and not pushViewController:animated:.
[self presentViewController:controller animated:YES completion:nil];
Use this Code in "didSelectRowAtIndexPath"
UIViewController *controller = [[UIViewController alloc]initWithNibName:#"UIViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[viewController release];
LoginViewSimpleController *loginViewSimple = [[LoginViewSimpleController alloc]init];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginViewSimple];
[self setNavigationController:navController];
Have you identify both tableviews ? Can you Post log status here ?

Passing a NSString from View to another

I am trying to pass a NSString to another view.
In my first view (is a map view) my code is:
FirstView.m file:
import "FlipsideViewController.h"
-(IBAction) displayDetails:(id) sender{
MyLocation *ann = [_mapView.selectedAnnotations objectAtIndex:([_mapView.selectedAnnotations count]-1)];
FlipsideViewController *flipsideViewController;
flipsideViewController= [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(#"ann.name:%#", ann.name);
NSLog(#"details:%#", flipsideViewController.details);
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"details" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
and the NSlog print the correct string. But when I try to pass that string to my SecondViewController string called details:
In my FlipsideViewController.h is defined:
NSString *details;
#property (nonatomic, strong) NSString *details;
While in the FlipsideViewController.m
#synthesize details; //and whenever I put the log, it prints the null value
value: NSLog(#"**************detalli %#",detalli);
Where is my fault?
in the secondViewController add a property, a retained property of type NSString
For adding properties please read the following
http://cocoacast.com/?q=node/103
Please edit this section
FlipsideViewController *flipsideViewController;
flipsideViewController= [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(#"ann.name:%#", ann.name);
NSLog(#"details:%#", flipsideViewController.details);
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"details" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
to
FlipsideViewController *flipsideViewController;
flipsideViewController= [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(#"ann.name:%#", ann.name);
NSLog(#"details:%#", flipsideViewController.details);
flipsideViewController.delegate = self;
flipsideViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:flipsideViewController animated:YES];
[flipsideViewController release];
try this step:-
1)Firstly You make the property in anotherViewController.h
#property (nonatomic, strong ) NSString *fetchStr;
2) Synthesis the NSString in anotherViewController.m
#synthesize fetchStr;
3)use in firstViewController
anotherViewController *avc = [[anotherViewController alloc]init];
avc.fetchStr = ann.name;
and print(NSLog) the fetchStr in anotherViewController
made annotation
#property(nonatomic,retain) MyLocation *annotation;
and then pass it to another view controller.
it will work.

How to access to parentViewController after presentModalViewController?

I need to access to parentViewController after presentMoalViewController.
This is my method that I call in the firstViewController to view the secondViewController:
- (void)viewData {
SecondViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:#"select_data"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:#selector(saveData)] autorelease];
viewCtrl.navigationItem.rightBarButtonItem = salvaButton;
UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:#"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:#selector(backView)] autorelease];
viewCtrl.navigationItem.leftBarButtonItem = annullaButton;
[self presentModalViewController:navigationController animated:YES];
}
When I click on saveButton, I try to access to parentViewController in this way, but it not work:
- (void) saveData {
FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
parentView.dataString = [[NSMutableString alloc] initWithFormat:#"new string"];
[parentView performSelectorInBackground:#selector(myMethod) withObject:nil];
[self dismissModalViewControllerAnimated:YES];
}
It would be much better if you used a delegate/protocol in your second viewController that the first viewController could set itself as. You can find more info here Pass data back to the previous controller or simply doing a search. This design pattern is used virtually everywhere in iOS programming.
The big advantage is that then your second viewController becomes independent of whoever presented it, and can be easily reused. The technical term would be 'decoupling'.
Last time I did this I used notifications:
[[NSNotificationCenter defaultCenter] postNotificationName:#"saveData" object:dataString];
in your parent vc view didload:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(saveDataNow:) name:#"saveData" object:nil];
For navigationController you can declare a property called something like myController, then you have to initiate your navigationController like this:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.myController = self;
UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:#selector(saveData)] autorelease];
viewCtrl.navigationItem.rightBarButtonItem = salvaButton;
UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:#"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:#selector(backView)] autorelease];
viewCtrl.navigationItem.leftBarButtonItem = annullaButton;
[self presentModalViewController:navigationController animated:YES];
In your parentViewController Class you declare your Method
- (void) saveData {
FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
parentView.dataString = [[NSMutableString alloc] initWithFormat:#"new string"];
[parentView performSelectorInBackground:#selector(myMethod) withObject:nil];
[self dismissModalViewControllerAnimated:YES];
}
In your navigationController you can then call [myController saveData];
This should work, if you have any questions, feel free to ask!
You can access your parent view controller using following-
- (void) saveData {
NSMutableArray *activeControllerArray = [self.navigationController.viewControllers mutableCopy];
FirstViewController *parentView;
for(int i = 0, i <[activeControllerArray count], i++) {
if([[activeControllerArray objectAtIndex:i] isKindOfClass:[FirstViewController class]) {
parentView= [activeViewController objectAtIndex:i];
break;
}
}
parentView.dataString = [[NSMutableString alloc] initWithFormat:#"new string"];
[parentView performSelectorInBackground:#selector(myMethod) withObject:nil];
[self dismissModalViewControllerAnimated:YES];
}

second view call from first view

I want to call secondview from my current view but this code suggested by someone is not working properly
UINavigationController *nav=[[UINavigationController alloc] initWithNibName:#"Second" bundle:[NSBundle mainBundle]];
[nav pushViewController:#"Second" animated:YES];
Why are you pushing an NSString onto a UINavigationController? I think what you want is:
SecondView * vc = [[SecondView alloc] initWithNibNamed:#"SecondView"];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
Hope that Helps!
You can use the presentModalViewController for the second view.
hope this will help you.
[self.navigationController presentModalViewController:SecondView animated:YES];
I suggest you put naviagtionbar initialization in your application:didFinishLaunchingWithOptions: if it's your rootViewController. You can initiate it with rootViewController, like so:
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:firstViewController_1];
self.window.rootViewController = nav;
[nav release];
Push other views in your view controller(e.g. your FirstViewController.m):
SecondViewController * secondViewController_2 = [[[NSBundle mainBundle] loadNibNamed:#"SecondViewController" owner:self options:nil] lastObject];
// Or: SecondViewController * secondViewController_2 = [[SecondViewController alloc] init...] autorelease];
[self.navigationController pushViewController:secondViewController_2 animated:YES];

TabBarController isKindOfClass - problem with UINavigationController

I have a TabBar with ViewController in it. I do this in my AppDelegate. So I have one UINavigationController
test1ViewController = [[Test1ViewController alloc] init];
test2ViewController = [[Test2ViewController alloc] init];
test3ViewController = [[Test3ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: test2ViewController];
NSArray* controllers = [NSArray arrayWithObjects: test1ViewController, navigationController, test3ViewController, nil];
[self.tabBarController setViewControllers:controllers animated:YES];
[navigationController release];
Now I have the problem with this line of source code:
[(Test2ViewController *)[appDelegate.myTabBarController selectedViewController] methodName:arg1 withTag:arg2];
Here there will be a SIGBRT, because the selectedViewController is in this case an "UINavigationController". But I want to call a method of the "Test2ViewController". How could I do this?
Normally I also do this:
if([[appDelegate.myTabBarController selectedViewController] isKindOfClass:[Test2ViewController class]]) { ... }
But this also fail because it is a UINavigationController. How to fix that? Does anyone know?
Thanks a lot in advance & Best Regards.
Try the following:
UINavigationController *navController = (UINavigationController *) [appDelegate.myTabBarController selectedViewController];
Test2ViewController *viewController = (Test2ViewController *) [[navController viewControllers] objectAtIndex: 0];
[viewController methodName:arg1 withTag:arg2];