I'm a beginner at C# so make sure you explain everything.
Okay, so I have a UIPickerView with its corresponding array of items in my ViewController. When the user clicks "submit", the app is supposed to take them to a SecondViewController, and display the item that they chose in the UIPickerView in a label. The only problem I am having is that I can't seem to link the two ViewControllers together.
I imported my ViewController by using #import into the SecondViewController, but that didn't work. I am getting a "use of undeclared identifer' error. What am I supposed to do to link the two ViewControllers together?
Thanks in advance.
If your first ViewController is the one that's presenting your second ViewController, then you can add a property in the second-view-controller that will be set to the selected value.
In SecondViewController.h
#property (nonatomic) NSString *selectedItemName;
Then, when you're presenting your second-view-controller, also set the value for this property:
SecondViewController *vc2 = [[SecondViewController alloc] init];
vc2.selectedItemName = #"selected item"; //set the actual selected item's value here
[self presentViewController:vc2 animated:YES];
Refer to this answer by Parth Bhatt. I think that should help you:
Passing NSString value between classes
I am re-pasting the answer to make the answer useful in case the link goes off in the future.
Answer from the above link:
Let us use the names of the two viewControllers as FirstViewController and SecondViewController.
Now suppose you push SecondViewController from FirstViewController on a button click then you need to write this code under your button click event:
// In the FirstViewController
- (void)buttonClicked:(id)sender{
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle: nil];
second.secString = firstString;
[self.navigationController pushViewController:second animated:YES];
[second release];
}
Here you need to declare in SecondViewController.h file:
NSString *secString;
and then create a #property
#property (non atomic,strong) NSString *secString;
In SecondViewController.m you need to #synthesize the secString:
#synthesize secString;
Hence this way you also create a getter and setter for secString by creating property and synthesizing it.
Now you can easily access the secString and you can use it anywhere in SecondViewController.
Just to check Try and check if value of firstString is passed to secString, write following code on viewWillAppear: of SecondViewController
NSLog(#"secString: %#",secString);
Let me know if you need more help.
Hope this helps.
Related
i want to use one data string stored in extern string variable in one view controller, in the other view by calling that variable in second view controller. and want to print the variable's value in the text box present in second view controller on a button click of button present in it.
please tell me how i can do that using objective-C?
please tell what to do in all the 4 files that are: ViewController.h, ViewController.m, SecondViewController.h and SecondViewController.m.
viewController.m
-(void)viewWillDisappear {
//pass value to secondViewController
secondViewController *vc=[[secondviewcontroller alloc]init];
vc.headStr=#"your string"; //same for array and dictionary
//Push code.........
}
//secondViewController.h
#property (strong, nonatomic) NSString *headStr;
secondViewController.m
-(void)ViewDidLoad
{
[super:ViewDidLoad];
Nslog("%#",self.headStr);
}
if i see your comment on Shameerjans post i see that you dont know what your doing.
Please do this tutorial from apple Start Developing iOS Apps
This is a cool jump in and covers very much functionality for displaying elements and using more controllers.
I think you need to pass one variable data into another view controller.
viewController.m
-(void)viewWillDisappear {
//pass value to secondViewController
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil]; // use your storyboard name
secondviewcontroller *vc = [storyboard instantiateViewControllerWithIdentifier:#"secondviewcontroller"];
vc.value2 = #"String here";
}
secondViewController.h
#property (nonatomic, retain) NSString *value2;
secondViewController.m
#implementation coachViewController
#synthesize value2;
-(IBAction)buttonPressed {
self.label.text = value2; //Declare value2 in .h file
}
How do I pass a value from one view controller to its previous prant view controller?
Consider this case: I have two view controller. The first screen has one lable and a button and the second view controller has one EditText and a back button.
If I click the first button then it has to move to second view controller and here user has to type something in the text box. If he presses the button from the second screen then the values from the text box should move to the first view controller and that should be displayed in the first view controller.
in secondViewController create protocol - SecondViewController.h
#import <UIKit/UIKit.h>
#protocol MyDelegate
-(void)PassString : (NSString *)str;
#end
#interface SecondViewController : UIViewController
#property (nonatomic)id <MyDelegate> delegate;
-(IBAction)btnBack:(id)sender;
than after in SecondViewController.m file
-(IBAction)btnBack:(id)sender
{
[delegate PassString:txt.text];
[self.navigationController popViewControllerAnimated:YES];
}
than after in FirstViewController set protocol of SecondViewController
in ViewController.h file
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#interface ViewController : UIViewController<MyDelegate>
than after in ViewController.m file
-(void)PassString:(NSString *)str
{
lbl.text=str;
}
You need to use a delegate for passing data from secondViewController to firstViewController:
- (IBAction)backToViewController:(id)sender {
[self.delegate viewController:self sendMessageToFirstViewController:#"EditText"];
}
This is and example for delegate.
This may help :
yourFirstViewController *viewController=(yourFirstViewController *)[self presentingViewController];
viewController.lbl.text=#"yourValue";
If you are using navigation controller to navigate, then use this :
yourFirstViewController *viewController=[self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2];
viewController.lbl.text=#"yourValue";
Basically you have to pass value from one view controller to another.Right ! You can use NSUserDefaults
Step 1. Save the text in an String
NSString *Text=#"Hello !"
[[NSUserDefaults standardUserDefaults] setObject:Text forKey:#"passingValue"];
[[NSUserDefaults standardUserDefaults]synchronize];
Step 2.
Now you have your textsaved in string "text".
Now lets pass this text into a view controller where you want.
NSString *savedtext =[[NSUserDefaults standardUserDefaults]
stringForKey:#"passingValue"];
Here you can display the text stored in "savedtext" into a textview or label.
All The Best
First you create one variable with property, ex.
#property (nonatomic, retain) NSString *strForUsername;
Then you
#import "SecondViewController".
Then you need to click event of the button.
Create Object of SecondViewController class, example
SecondViewController *mSecondViewController = [SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
mSecondViewController.strForUsername = #"Hello I m from first view controller";
[self.navigationController pushViewController:mHomeViewController animated:YES];
You should use variable with value in SecondViewController.
Leveraging RESideMenu https://github.com/romaonthego/RESideMenu in my app, now has an issue of passing some urlString related to lists in sideMenuViewControll to show its web page in right UIWebView.
Original codes like below:
case 2: {
navigationController.viewControllers = #[[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
[self.sideMenuViewController hideMenuViewController];
}
break;
"second" is for secondViewController that has a UIWebView for showing web page at right side. secondViewController has a property: urlString.
My intention is to show its web page when selecting "Profile". Tried several way, however, I can't find one to set urlString after navigationController.viewControllers = #[[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
How can I set and pass urlString so that it could be used like NSUrl * url = [NSURL URLWithString:urlString]?
First of all import your secondViewController in your first View Controller's .m file
Create in secondViewController's .h file
#property (strong, nonatomic) *NSString passedURL;
Then give the storyboard name in Attribute Inspector.
Now, on button event of profile or whatever
secondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
svc.passedURL = urlString;
[self presentViewController:svc animated:YES completion:nil];
Thats it. Hope that it works for you.
The Below line shows that,
navigationController.viewControllers = #[[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
You are trying to access the array of ViewControllers, but I am not sure why you want all the ViewControllers. I think you can get the secondViewController like this below and set the string
secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
secondViewController.urlString = urlString;
After tried every method I could get, find a workaround by using [navigationController.viewControllers[0] setTitle:urlStrings] might works.
Is doing such correctly?
Object: change the label's text in ViewControllerB
(In the ViewControllerA.m)
- (IBAction)ReturnToMenu
{
ViewControllerB *ViewControllerB =
[self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
ViewControllerB.scorelabel.text=#"WHY IS IT NOT WORKING";
[self presentViewController:GameOverViewController animated:YES completion:nil];
}
Well, this isn't working. I've exhausted all my options. This is such an elementary question, but please help me out.
P.S. #"Why is it not working" is just a temporary thing, a variable from ViewControllerA is supposed to take its place.
P.P.S. I tried out Passing Data between View Controllers, but it didn't work. I'm not sure what I'm doing wrong here
You should not try to manipulate another view controller's views directly. It's bad design, and in many cases (like yours) it doesn't work.
Instead of setting a label's text, add a string property "score". Set that after instantiating the VC.
Then, in "ViewController8"s viewWillAppear method, install the score property into the label.
Problem solved.
Here I present a modal view controller from my main view controller to show locations which I previously got from foursquare api.
LocationPickerVC *vc = (LocationPickerVC*)[self.storyboard instantiateViewControllerWithIdentifier:#"modal"];
if([vc respondsToSelector:#selector(setLocations:) ])
vc.locations = items;
[self.formSheetController presentViewController:vc animated:YES completion:nil];
[EDIT]
Make sure you set storyboard id in your storyboard (for the code below it is "modal"). Also my LocationPickerVC has this interface.
#interface LocationPickerVC : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property(strong, nonatomic) NSMutableArray *locations;
#end
OK, try this:
In your ViewController1.h that the property score is declared, add this:
+ (ViewController1 *) sharedController;
In your ViewController1.m add this method:
+ (ViewController1 *)sharedController
{
static ViewController1* staticVar = nil;
if (staticVar == nil)
staticVar = [[ViewController1 alloc] init];
return staticVar;
}
Now, where ever in other ViewControllers you need something from ViewController1, import the header, and then get the property like this:
[[ViewController1 sharedController] score]
Ok so I am trying to pass a string from one view controller to another via the AppDelegate. I want to stay on the current view while this happens.
This is the main body of the code I am currently using to do this:
AppDelegate *dataCenter = (AppDelegate *)[[UIApplication sharedApplication] delegate];
MyMealViewController *vc = [[MyMealViewController alloc] initWithNibName:nil bundle:nil];
dataCenter.selectedMenuItem = recipeLabel.text;
[self presentViewController:vc animated:YES completion:NULL];
When I run the program I am able to confirm that the string is correctly passed. However, then the view on the simulator just turns black. I assume that this is because initWithNibName is set to nil.
So my question is: how should I change my code so that the string will still be passed, but the current view will continue to be displayed on the iphone. Is there a line of code that I could write that would just reload the current view?
Thanks for your help with this issue. I am new to xcode so I may be making a very basic error. Please let me know if any additional information would be helpful in answering this question.
Edit: It looks like you want to show a list of food items in the first view. Tapping an items opens a detail view. From that detail view, the user can press a button to add it to the meal. Eventually, they can tap a button on the first view to open the meal view, which should contain all of the items that they selected.
If this is the case, keep an array on the first view controller, and make sure the detail (second) view controller has a reference to the first view controller when it is presented. This will let us use that array. Note that there are better ways to architect this, but this will work for now:
#interface FoodListViewController : UIViewController
#property (strong, nonatomic) NSMutableArray *foodItems
#end
#implementation FoodListViewController
- (void)showFoodItem
{
FoodItemDetailViewController *detailViewController = [[FoodItemDetailViewController alloc] initWithNibName:nil bundle:nil];
detailViewController.foodListController = self;
[self presentModalViewController:detailViewController animated:YES];
}
#end
Once the detail view is presented, tapping the 'add to meal' button should add the current 'mealItem' to the array. In your example, you were using strings - if you would rather keep an array of strings for some reason, I'll leave that to you.
#interface FoodItemDetailViewController : UIViewController
#property (nonatomic, weak) FoodItemsViewController *foodListController;
#end
#implementation FoodItemDetailViewController
- (IBAction)buttonTapped:(id)sender
{
[self.foodListController.foodItems addObject:self.mealItem];
// Update the UI to let the user know that the item was added to the meal
}
#end
Finally, when it comes time to present the MealDetailsViewController, just pass it the array that you have been building:
#interface MealDetailsViewController : UIViewController
#property (nonatomic, strong) NSArray *foodItems;
#end
#implementation MealDetailsViewController
// Set foodItems before this view controller is presented, then use it to drive the
// UITableView data source, or find some other way of displaying it.
#end
As you can see, both the second and third view controllers are presented by the first. View controllers (nearly) always form a hierarchy - so keeping your data at the top of that hierarchy (by storing it in FoodListViewController) lets you neatly pass it down the hierarchy as you present other view controllers.