I'm having a small problem with my push segue on Xcode Im trying to push the content of one label to another one on a different view controller, this is my current setup no errors are present and but the text on the other view controller doesn't change to match the previous window.
The first example below is my detailviewcontroller which I'm sending the information from. I have created the push segue between the detail view controller and my new view controller which is CODEVIEW and given the segue the name ShowCode.
I was wondering if I was missing line of code which would automatically activate this segues information without the need of a button but instead works when loaded.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"ShowCode"]) {
CODEVIEW *CodeSender = [segue destinationViewController];
CodeSender.CODElabel.text = _Code.text;
}
}
Label outlet for CODEVIEW
#property (strong, nonatomic) IBOutlet UILabel *CODElabel;
Label outlet for detail view controller
#property (strong, nonatomic) IBOutlet UILabel *Code;
In your current VC:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"ShowCode"]) {
CODEVIEW *CodeSender = [segue destinationViewController];
CodeSender.codeLabeltext = _Code.text;
}
}
In your target/next VC:
a) inside CODEVIEW.h interface:
#property (strong, nonatomic) NSString *codeLabeltext;
#property (strong, nonatomic) IBOutlet UILabel *CODElabel;
b) in your CODEVIEW.m implementation:
- (void)viewDidLoad {
[super viewDidLoad];
//...
_CODElabel.text = codeLabeltext;
//...
}
Related
I am trying to pass UITextField's text from View Controller 1 to the UILabel in View Controller 2.
I am using segue to pass the information, but I am not getting anything on Label. It seems like the text from the text field becomes NULL in when it is passed to view controller 2.
View Controller 1 (UITextField)
- (IBAction)sendtoVC2:(UIButton *)sender
{
[self performSegueWithIdentifier:#"toVC2" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toVC2"])
{
ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
VC2.label.text = self.textField.text;
}
}
View Controller 2.h (UILabel)
#import "ViewController1.h"
#interface ViewController2 : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *label;
#end
Thank you.
Most likely, it happens because the label doesn't exist yet. When UIViewController is created, its view is not loaded until it is actually required. This is called 'lazy loading', which means that a value is created only when somebody requires it for the first time.
Lazy loading is a design pattern commonly used in computer programming
to defer initialization of an object until the point at which it is
needed. It can contribute to efficiency in the program's operation if
properly and appropriately used.
Your UILabel property in second view controller gets a value only after second view controller's view is loaded, which happens only when somebody explicitly calls its 'view' property. So, to prove my theory just insert one more line:
if ([segue.identifier isEqualToString:#"toVC2"])
{
ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
UIView *unusedReferenceToViewToLoadTheView = VC2.view
VC2.label.text = self.textField.text;
}
In viewDidLoad of View Controller 2, the text is lost, because the label was not created yet.
You have to save the text in a variable, and in viewDidLoad of ViewController2, set the text in label.
Like this:
View Controller 1:
- (IBAction)sendtoVC2:(UIButton *)sender
{
[self performSegueWithIdentifier:#"toVC2" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toVC2"])
{
ViewController2 *VC2 = (ViewController2 *)segue.destinationViewController;
VC2.text = self.textField.text;
}
}
View Controller 2.h:
#import "ViewController1.h"
#interface ViewController2 : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *label;
#property (nonatomic, copy) NSString *text;
#end
View Controller 2.m:
- (void)viewDidLoad {
[super viewDidLoad];
label.text = text;
}
i have many view controller in my storyboard some how i am passing my login page data email id to my third view controller but i am getting the NULL value
here is my code for 1st view
//in first view i already import my second view below code is in 1st view
//.h
#property (weak, nonatomic) IBOutlet UITextField *emailid;
//.m
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"sendemailid"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
UploadImageData *controller = (UploadImageData *)navController.topViewController;
controller.newemailid = self.emailid.text;
NSLog(#"emailid is %#",emailid.text);
}
}
// i already created segue by identifier name "sendemailid"
now in my second view
// in .h
#property (strong, nonatomic) IBOutlet UILabel *emailid;
#property (nonatomic) NSString *newemailid;
// .m in view didload method
self.emailid.text = self.newemailid;
NSLog( #"we get the emailid %#", emailid.text );
i am getting null value and one more thing i am using NSUSER DEFAULT to save emailid in 1st view. i don`t able to know why value is not passing is that i am missing something please suggest...
I have done with this
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"sendemailid"])
{
UploadImageData *controller=segue.destinationViewController;
controller.newemailid=self.emailid.text;
}
}
I have been having trouble with what seems to be a fairly simple problem but I can't for the life of me work it out.
My initial view is a single view not embedded in anything where the user logs in. After they press a button I want to transition to a view that is part of a 2 tabbed UITabBarController that is also embedded in a UINavigationController. I have tried every type of Segue but I either get a NSInvalidArgumentException with the UITabBarController or it takes me to the next view but the tab bar and the navigation bar do not appear on the screen.
Hopefully this is detailed enough to understand what I am trying to do, if it is not I can upload a picture of my storyboard.
Edit - Here is my storyboard and my prepareForSegue method
Storyboard : http://oi60.tinypic.com/2pyub0o.jpg
#property (nonatomic) int numberOfPlayers;
#property (strong, nonatomic) UITabBarController *tbc;
#property (strong, nonatomic) MPAMainViewController *mvc;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"startGameSegue"]){
self.numberOfPlayers = [[self.textField text] intValue];
self.tbc = (UITabBarController *) [segue destinationViewController];
self.mvc = [self.tbc.viewControllers objectAtIndex:0];
self.mvc.numberOfPlayers = self.numberOfPlayers;
}
The error I am currently encountering is "-[UINavigationController setNumberOfPlayers:]: unrecognized selector sent to instance 0x7fc103493380"
Thanks!
I am getting an error while passing data from a table view controller to a detail view
controller. I am using a navigation controller along with it.
The error message is as follows:
use of undeclared identifier _wordLabel
This is the prepare for segue implementation in my table view controller (.m file).
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowWordDetails"]) {
//UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
wordDetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
long row = [myIndexPath row];
detailViewController.wordDetailModel = #[_wordLabel[row], _meaningLabel[row]];
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
I have previously declared wordLabel and meaningLabel in the detail view controller as follows:
#property (strong, nonatomic) IBOutlet UILabel *wordLabel;
#property (strong, nonatomic) IBOutlet UILabel *meaningLabel;
#property (strong, nonatomic) NSArray *wordDetailModel;
I have also declared it in viewDidLoad method of detail view controller as follows:
-(void)viewDidLoad {
[super viewDidLoad];
_wordLabel.text = _wordDetailModel[0];
_meaningLabel.text = _wordDetailModel[1];
// Do any additional setup after loading the view.
}
_wordLabel is private member of your detailViewController. You can't access it like you're trying to do in the first code quote. You need to call detailViewController.wordLabel instead.
first of all you can't use the array operator with a UILabel
_wordLabel[row]
second of all you have to define the _wordLabel[row] as an array in the TVC, that is separate from the label in the detailViewController
I think you don't require wordDetailModel as you can access UILabel from TableViewController as described below
detailViewController.wordLabel.text = #"Word";
detailViewController.meaningLabel.text = #"Meaning";
_wordLabel is UILabel and it is private variable in DetailViewController which can not be used directly from TableViewController, you have to use it using property. Plus _wordLabel is not an array as already answered by #sha and #rockstarr
I have a Navigation Controller and 2 Views Controllers. The first View Controller is associated with a UIViewController named ViewController. The second is connected to a UIViewController named BookVC. BookVC has a UITextField and is connected via an outlet:
#property (strong, nonatomic) IBOutlet UITextField *textFieldContent;
The connection is made with a button that uses a segue. I want to pass some data between the two and am using the following code which fails:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
BookVC* nextPage = [[BookVC alloc] init];
nextPage = [segue destinationViewController];
nextPage.textFieldContent.text=#"Some content";
}
How should I pass data between the View Controllers?
I think the problem is that textFieldContent doesn't exist at that point. You'll need to add a property to BookVC that can hold the text you want to put into textFieldContent... we'll call it #property NSString *textFieldContentText. So then,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
BookVC *nextPage = [segue destinationViewController];
[nextPage setTextFieldContentText:#"Some content"];
...
}
And then, in the -viewDidLoad method of BookVC:
- (void)viewDidLoad
{
[textFieldContent setText:[self textFieldContentText]];
}
You don't need this line:
BookVC* nextPage = [[BookVC alloc] init];
That creates a brand new BookVC which you then go and overwrite in the next line.
Since you are using [segue destinationViewController] you should be good to go. What's happening when you segue to nextPage?