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.
Related
I am creating an iphone application, where i have number of grid views in a main view controller class. So, i want when i select a grid cell from main view, should load a Reveal controller. The layout looks like
I have the following code in my Main View Controller.
-(void) gridView:(UzysGridView *)gridView didSelectCell:(UzysGridViewCell *)cell
atIndex:(NSUInteger)index
{
if (index == 0){
FrontViewController *frontViewController = [[FrontViewController alloc]
init];
RightViewController *rightViewController = [[RightViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc]
initWithRootViewController:frontViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:nil
frontViewController:frontNavigationController];
mainRevealController.delegate = self;
self.revealController.rightViewController = rightViewController;
self.revealController = mainRevealController;
[self.navigationController pushViewController:self.revealController animated:YES];
}
here in main view controller .h file,
#property (strong, nonatomic) SWRevealViewController *revealController;
Also i have declare SWRevealViewControllerDelegate. Problem is that this self.revealController doesn't load/show. I have tried with App Delegate also, but nothing works.
thanks.
I solved it: Use the below code..
-(void) gridView:(UzysGridView *)gridView didSelectCell:(UzysGridViewCell *)cell
atIndex:(NSUInteger)index {
if (index == 0){
FrontViewController *frontViewController = [[FrontViewController alloc]
init];
myNavigationController = [[UINavigationController alloc]
initWithRootViewController:frontViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc]
initWithRearViewController:nil frontViewController:myNavigationController];
revealController.delegate = self;
RightViewController *rightViewController =
[[RightViewController alloc] init];
rightViewController.view.backgroundColor = [UIColor greenColor];
revealController.rightViewController = rightViewController;
self.viewController1=revealController;
[self presentViewController:self.viewController1 animated:YES completion:nil];
}
}
And in Class.h file
#property(nonatomic, strong) UINavigationController *myNavigationController;
#property(nonatomic, strong) SWRevealViewController *viewController1;
My app has an variable called int antalratt, which is the number of correct answers in that view. Now I want to pass that variable to the next view, where I want to get the number of correct answers to be shown! I know how to get an integer to a label text though!
The int antalratt is written in the firstviewcontroller.m, how do I make it "global" so that I can use it in the secondviewcontroller?
Thanks in advance!
make a variable in the public interface of secondviewcontroller.h
#property (nonatomic, strong) NSNumber *correctAnswers;
synthesize it in .m and then pass the value of antalratt in firstviewcontroller with secondviewcontroller.correctAnswers = [NSNumber numberWithInt:antalratt];
to secondviewcontroller. then set the labeltext
Method 1:
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
fvc.answer = antalratt;
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
#interface FirstViewController : UIViewController
{
int answer;
}
#property(nonatomic,assign) int answer;
#implementation FirstViewController
#synthesize answer;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%d",answer); // //displays answer on log
}
#end
Method 2 (AppDelegate)
AppDelegate
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
int antalratt;
}
#property(nonatomic ,assign) int antalratt;
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
delegate.antalratt = antalratt;
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
NSLog(#"%d",delegate.antalratt); //displays answer on log
}
Method 3 (NSUserDefaults)
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
[[NSUserDefaults standardUserDefaults] setInteger:antalratt forKey:#"answer"];
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release]; }
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
int ans = [[[NSUserDefaults standardUserDefaults] objectForKey:#"answer"] intValue];
NSLog(#"%d",ans); //displays answer on log
}
filename *detailViewController = [[filename alloc] initWithNibName:#"filename" bundle:nil];
detailViewController.audio=#"yourData";
[self presentModalViewController:detailViewController animated:YES];
[detailViewController release];
Declare in filename.h
NSString *audio;
#property(nonatomic,retain) NSString *audio;
and filename.m
#synthesize audio;
-(void) ViewDidLoad
{
NSLog(#"Audio = %#",audio); // if ur variable is integer declare %d in nslog.
}
thats all
//
view1.h
#interface view1 : UIView{
NSString *passingVariable;
}
#property (nonatomic, strong) NSString *passingVariable;
#end
//
view1.m
#synthsize passingVariable;
#implementation view1
#end
// in another view
view2.m
#import "view1.h"
#implementation view2
-(IBAction)changeview
{
view1 *myview = [[view1 alloc]init];
myview.passingVariable = [NSString stringWithString:#"Hello Variable"];
[self.navigationController pushViewController:myview animated:YES];
}
#end
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];
On my iPhone app, I have two viewControllers (firstViewController, secondViewController), on firstViewController the user can select a photo from the camera roll and it then displays it in an imageView, however I need to also display it in secondViewController but have no idea how to.
Can you also please explain your answers in-depth as I am fairly new to objective-C
Here's my code:
firstViewController.h
#import <UIKit/UIkit.h>
#interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImageView *theImageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;
#end
firstViewController.m
#import "firstViewController.h"
#import "secondViewController.h"
#implementation firstViewController
#synthesize theImageView
-(IBAction) selectExistingPicture
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
theImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}
// Default Apple code
#end
There's not much in secondViewController so I won't bother posting that code.
Have a method in your second view controller of the type
-(void)setImage:(UIImage *)image
In the method below after you create a viewcontroller call the method with the image as shown
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller setImage:theImageView.image]
[viewcontroller release];
}
You need to declare a property in second view so you can set the image from your first view some thing like this:
secondImageView is the property in secondView and you have to set it
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewcontroller.secondImageView.image = firstViewImage;
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}
I have an array initialized in my RootViewController and a method that addsObjects to an array. I created a RootViewController object in my SecondViewController. The method runs (outputs a message) but it doesn't add anything to the array, and the array seems empty. Code is below, any suggestions?
RootViewController.h
#import "RootViewController.h"
#import "SecondViewController.h"
#implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
myArray2 = [[NSMutableArray alloc] init];
NSLog(#"View was loaded");
}
-(void)addToArray2{
NSLog(#"Array triggered from SecondViewController");
[myArray2 addObject:#"Test"];
[self showArray2];
}
-(void)showArray2{
NSLog(#"Array Count: %d", [myArray2 count]);
}
-(IBAction)switchViews{
SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
[screen release];
}
SecondViewController.m
#import "SecondViewController.h"
#import "RootViewController.h"
#implementation SecondViewController
-(IBAction)addToArray{
RootViewController *object = [[RootViewController alloc] init];
[object addToArray2];
}
-(IBAction)switchBack{
[self dismissModalViewControllerAnimated:YES];
}
EDIT*************
With Matt's code I got the following error:
" expected specifier-qualifier-list before 'RootViewController' "
You are missing some very essential fundamentals here. If you allocate a new RootViewController in your SecondViewController, it is not the same instance as the one you used to create your SecondViewController so it will not have a reference to the array you are adding objects to. What you are trying to do won't work. You must create an ivar in your SecondViewController for your RootViewController and then access it inside second view. Something like this:
-(IBAction)switchViews{
SecondViewController *screen = [[SecondViewController alloc]
initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[screen setRootViewController:self];
[self presentModalViewController:screen animated:YES];
[screen release];
}
Your ivar would need declared like this in the SecondViewController.h:
#property (nonatomic, retain) RootViewController *rootViewController;
And then synthesized in the .m
Then, you can access the ivar from within your SecondViewController:
-(IBAction)addToArray{
[[self rootViewController] addToArray2];
}