I am new to the objective C programming and I am in a position where I need to create an iPhone App really quickly.
I am using XCode 4.2
I have a problem transferring an NSString variable from one view to the other .
the two views are in two different sets of .h and .m classes
in the first class in the .h i have something like this
#interface firstview : UIViewController {
NSString *test;
}
-(IBAction)testbutton
#end
in the .m of the firstview I have
-(IBAction)testbutton{
secondView *second;
[second setText:text]; //set text is a function that will take an NSString parameter
second= [[secondView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
in the .h of the secondView I wrote
#interface secondView : UIViewController{
-IB
}
You've got the right idea, but you're trying to call -setText: before second points to a valid object! Do this instead:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
Also, the interface you give for your secondView class looks both incorrect and incomplete -- I'm not sure what you're trying to do with the -IB part. And it'd help in the future if you follow the usual Objective-C naming convention and start class names with an uppercase character: SecondView instead of secondView. Finally, I'd advise against naming a view controller ending in "...View", since that makes it easy to confuse the view controller with a UIView. All together, it should look something like this:
#interface SecondViewController : UIViewController{
NSString *text;
}
#property (retain, nonatomic) NSString *text;
#end
Declaring text as an instance variable is optional there -- if you don't do it, the compiler will create an ivar if you synthesize the accessors for your text property.
Change your code to this:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
In your original version, you were initializing (i.e. instantiating) your second view after calling setText: on it. You need to initialize it and then set the text.
You need to set the text after you allocate and initialize.
-(IBAction) testButton {
secondView *second = [[[secondView alloc] initWithNibName:nil bundle:nil] autorelease];
[second setText:text];
[self presentModalViewController:second animated:YES];
}
Related
I want to make one app and it include form submition . In this i create one new project by single view application. In this firstViewController I have put button and by pressing it , it redirect to secondViewController.
In this i have a form. and want to take data from user and by submit button i want to redirect this to thirdViewController and want to print the form data on thirdViewController.
In firstViewController.m file i have written
-(IBAction)nextVCButton:(id)sender
{
SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
NSLog(#"name : %#",self.nameTextField.text);
tempView.fullName = self.nameTextField.text;
[tempView setFullName:self.fullName];
[self.view addSubview:tempView.view];
}
In SecondViewController.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"received info %#", [self.fullName description]);
self.nameLabel.text = [self.fullName description];
}
In log i am getting (name :) value but in secondViewController received info is null.
I am doing this all by using xib file. No use of Storyboard.
Please help me to get this form data to secondViewController.
Thanks.
Sorry , I couldn't get why you have done these two line code :
tempView.fullName = self.nameTextField.text;
[tempView setFullName:self.fullName];
Instead ,
Make a property of NSString in Second View Controller's .h file
#property (nonatomic,retain) NSString *strFullName;
and synthesize in .m file.
#synthesize strFullName;
Now ,
-(IBAction)nextVCButton:(id)sender
{
SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
NSLog(#"name : %#",self.nameTextField.text);
tempView.strFullName = self.nameTextField.text;
[self.view addSubview:tempView.view];
}
I think you need to use Navigation controller if you want to navigate from one view controller to another view controller. well here you need to do property- synthesize the iVar "fullName" in ThirdViewController. and retain the value in SecondViewController .
1. ThirdViewController.h
#property(nonatomic, retain) NSString *fullName;
2. ThirdViewController.m
#synthisize fullName;
dealloc {
[fullName release];
}
3. SecondViewController.m
-(IBAction)nextVCButton:(id)sender
{
SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
NSLog(#"name : %#",self.nameTextField.text);
tempView.fullName = self.nameTextField.text;
[tempView setFullName:self.fullName];
[self.view addSubview:tempView.view];
}
try presentViewController method instead of addSubview....
-(IBAction)nextVCButton:(id)sender
{
SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
NSLog(#"name : %#",self.nameTextField.text);
tempView.strFullName = self.nameTextField.text;
[self.view addSubview:tempView.view];
[self presentViewController:tempView animated:YES completion:nil];
}
Any one have exact solution for passing image from one view to other view when using popview controller in iPhone.
Please give me some guidelines for this problem.
Thanks.
Using Protocol Delegate method use the image, or else assign your image to another controller image object and use it.
suppose your imageview is existingImageView;
.h
#interface SecondController : UIViewController
#property (retain, nonatomic) UIImageView *previousImageView;
.m
#interface SecondController ()
#end
#implementation SecondController #synthasize previousImageView;
Now in firstController while navigating use it like,
SecondController *second = [[SecondController alloc] init];
[self.navigationController pushViewController:second animated:YES];
[second setPreviousImageView:existingImageView];
[photoController release];
After Cropping Image it will reflect to existingImageView.
I have one view who calculates location and reverser geocode to get the zip code. Then it calls another view where I want to display weather results based on that zip code.
In the first view controller I do this once user clicks on the button to turn the page:
- (IBAction) showMyWeather : (id)sender {
WeatherApp *weather = [[WeatherApp alloc] initWithNibName:nil bundle:nil];
weather.zipcode = placemarkZip; //this one seems not to be doing the job
weather.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:weather animated:YES];
}
And at the WeatherApp I would like to read now zipcode, which is declared in this view controller .h:
#interface WeatherApp : UIViewController{
IBOutlet UIButton *done;
MKPlacemark *zipcode;
}
#property (nonatomic, retain) MKPlacemark *zipcode;
How can I use this code to transfer this zipcode to the WeatherApp? Thanks!
Yes, this is a fine way to pass information into your new object.
Alternatively, you could create a custom initializer for WeatherApp like
- (id)initWithZipCode:(NSString *)zip;
and then in the implementation file, it could be like this:
- (id)initWithZipCode:(NSString *)zip
{
self = [super init];
[self setZipcode:zip];
return self;
}
Finally, you could instantiate the class like so:
- (IBAction)showMyWeather:(id)sender
{
WeatherApp *weather = [[WeatherApp alloc] initWithZipCode:placemarkZip];
[weather setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:weather animated:YES];
[weather release]; // No longer needed with ARC... just sayin'
}
Finally, if you're going to continue to pass in information as you do above, I'd question why you're using initWithNibName:bundle:. If you're just going to pass nil to both, why not just use [[WeatherApp alloc] init]?
EDIT: Added Source Project
--> I uploaded a sample project that clearly shows my dilemma which can be found here <--
I created a Split View-based Application. I then added a second UINavigationController to the DetailViewController inside the MainWindow.xib.
I then pop a new UIViewController Subclasses when a toolbar item is clicked. I use the following code to conduct the pop:
DocumentDetailVC *DetailViewController = [[DocumentDetailVC alloc] initWithNibName:#"DocumentDetailVC" bundle:[NSBundle mainBundle]];
[detailViewController.detailNavController pushViewController:DetailViewController animated:YES];
DocumentsVC *RRequestViewController = [[DocumentsVC alloc] initWithNibName:#"DocumentsVC" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:RRequestViewController animated:YES];
This works. The issue I am having is how do I pass information or function calls from the Main side of the Split View to the Detail side of the Split view?
If I present the UIViewController via the following method:
DocumentDetailVC *RRequestViewController = [[DocumentDetailVC alloc] initWithNibName:#"DocumentDetailVC" bundle:[NSBundle mainBundle]];
RRequestViewController.delegate=self;
RRequestViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[RRequestViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:RRequestViewController animated:YES];
[RRequestViewController release];
RRequestViewController = nil;
I am able to complete a reach back through a protocol as intended.
DocumentDetailVC, when loaded via the pushViewController, the hierarchy is as follows:
NSLog([NSString stringWithFormat:#"%#",self]);
//self = <DocumentDetailVC: 0x4e0d960>
NSLog([NSString stringWithFormat:#"%#",self.parentViewController]);
//self.parentViewController = <UINavigationController: 0x4e0ce30>
NSLog([NSString stringWithFormat:#"%#",self.parentViewController.parentViewController]);
//self.parentViewController.parentViewController = <UISplitViewController: 0x4e0d0f0>
Thank you for your assistance. This problem is consuming my life!
--> I uploaded a sample project that clearly shows my dilemma which can be found here <--
Ok, I know am very late for answer but this answer is, I think, perfect and working. Try it. Open your RootViewController.h, on the top write this line:
#import "Left.h"
#import "Right.h"
in #interface RootViewController write this lines
Left *lefty;
Right *righty;
after that declare property as,
#property (nonatomic, retain) Left *lefty;
#property (nonatomic, retain) Right *righty;
go to ROOTVIEWCONTROLLER.M file synthesize as,
#synthesize lefty;
#synthesize righty;
after that in RootViewController.m just replace your function with this one
- (IBAction)myBtnPushed{
NSLog(#"myBtnPushed");
lefty = [[Left alloc] initWithNibName:#"Left" bundle:[NSBundle mainBundle]];
righty = [[Right alloc] initWithNibName:#"Right" bundle:[NSBundle mainBundle]];
lefty.delegate=righty;
[self.navigationController pushViewController:lefty animated:YES];
[detailViewController.navigationController pushViewController:righty animated:YES];
}
in delloac write this,
[lefty release];
lefty = nil;
[righty release];
righty = nil;
It's done, run your app. I tested, it's done.
I'm using two UIViewController in Application Delegate and navigating to UIViewController using presentmodalviewcontroller. But Problem is that presentmodalviewcontroller works for first time UIViewController and when i want to navigate to second UIViewController using presentmodalviewcontroller then its showing first UIViewController.
The following is the code:-
-(void)removeTabBar:(NSString *)str
{
HelpViewController *hvc =[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:[NSBundle mainBundle]];
VideoPlaylistViewController *vpvc =[[VideoPlaylistViewController alloc] initWithNibName:#"VideoPlaylistViewController" bundle:[NSBundle mainBundle]];
if ([str isEqualToString:#"Help"])
{
[tabBarController.view removeFromSuperview];
[vpvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:hvc animated:YES];
[hvc release];
}
if ([str isEqualToString:#"VideoPlaylist"])
{
[hvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:vpvc animated:YES];
[vpvc release];
}
}
Can Somebody help me in solving the problem?
You're making a new hvc and vpvc each time you run this function.
The first time through, I assume you call removeTabBar:#"Help", it makes a hvc and vpvc and then shows the correct one.
The second time you call it removeTabBar:#"VideoPlayList", you are making a new hvc and vpvc. This means that when you call hvc dismissModalViewController:YES]; you're not removing the one you added before, you're removing the new one that you just made which isn't being displayed at all!
To solve this you need to make your two controllers as properties in your app delegate and create them in the applicationDidFinishLaunching method.
Add these into your app delegate's .h file:
#class MyAppDelegate {
HelpViewController *hvc;
VideoPlaylistViewController *vpvc;
}
#property (nonatomic, retain) HelpViewController *hvc;
#property (nonatomic, retain) VideoPlaylistViewController *vpvc;
#end
and in your app delegate's .m file :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
self.hvc = [[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:nil] autorelease];
self.vpvc = [[[VideoPlaylistViewController alloc] initWithNibName:#"VideoPlaylistViewController" bundle:nil] autorelease];
...
}
and remove the first two lines in removeTabBar