how to use label instead of button in prepareforsegue in swift
first give the segue an identifier in storyboard and on label click or tap method use performSegueWithIdentifier("identifier", sender: self)
. This way the prepareForSegue gets called even on tapping label (Note: userInteraction should be enabled on label)
In your code replace self.presentViewController(vc as! UIViewController, animated: true, completion: nil)
with self.performSegueWithIdentifier("identifier", sender: self)
Cannot change label text after performing segue
Because you're accessing the views of recievingVC
, that means viewDidLoad()
will be called (hence the name view did load). And after viewDidLoad()
is called, you change the label again to 673. To fix this, you could change the text in viewWillAppear()
.
class StatisticsVC: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let potato = view.subviews[0] as! StatisticsView // You should just have "potato" as a property
potato.GamesWonNum.text = "580" // GamesWonNum should be called "gamesWonNum"
}
}
class TestViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "statsSegue" {
segue.destination.view.subviews.forEach { ($0 as? StatisticsView)?.setLabel(data: "673") }
}
}
}
How to prepare for segue and send data from 1 label to another?
Change in this:
PC.label1.text = Label.text!
You are trying to assign a String to a variable of type UILabel
instead of its text value.
Prepareforsegue set label
You're casting the sender
to two very different things.
UIImage *image = (UIImage*)sender;
ShowImageViewController *viewController = segue.destinationViewController;
viewController.pickedImage = image;
UILabel *label = (UILabel *) sender;
ShowImageViewController *vc = segue.destinationViewController;
vc.cap = label;
label.text = @"Hello";
You cannot cast a UIImage to a UILabel and expect things to go smoothly. In prepareForSegue
, you send along an image, not a label.
Can you just change the bottom four lines to:
viewController.cap.text = @"Hello";
In this case, another option would be to define a new property in ShowImageViewController
as you did for the image.
//SecondViewController.h
@property(nonatomic, strong) UIImage *pickedImage;
@property (weak, nonatomic) IBOutlet UIImageView *pickedImageView;
@property(nonatomic, retain) IBOutlet UILabel *cap;
@property (nonatomic, strong) NSString pickedLabel; //New property
-(void)viewWillAppear:(BOOL)animated{
self.pickedImageView.image = self.pickedImage;
self.cap.text = self.pickedLabel;
}
And then in prepareForSegue
UIImage *image = (UIImage*)sender;
ShowImageViewController *viewController = segue.destinationViewController;
viewController.pickedImage = image;
viewController.pickedLabel = @"Hello";
Send label text data with segue in TableView in Swift
I've finally done that using this example: Passing data between two UIViewControllers using Swift and Storyboard
It's make a lot easier and helps me finally get it to work!
Thanks!
How can I set the value of a UILabel following a segue?
Your UILabel text is not being set because prepareForSegue
is being called before the IBOutlet for your label on the destination view controller is connected. So at that point the label is still nil
(and sending a message to nil
has no effect.)
What you should do instead is to create a new property on your destination view controller to store the string, and set this property in prepareForSegue
. Then in your destination view controller's viewDidLoad
method, use the value of that property to set the text
property of your UILabel.
Unexpectedly found nil IBOutlet in prepareForSegue
You cannot access IBOutlet
s of DetailViewController
in prepareForSegue
because they are not connected yet.
As mentioned in the comments create a String
property in DetailViewController
, set it in prepareForSegue
and set the text
property of the label in viewDidLoad
or viewWillAppear
of DetailViewController
.
setting destinationviewcontroller's label's text in prepareForSegue throws error: unexpectedly found nil while unwrapping Optional value
This is because the outlet for myLabel
will not get set in prepareForSegue
, so it will be nil. Try below approach instead,
create a string var in DetailViewController
like,
var labelText: String?
in prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get reference to the destination view controller
var detailVC = segue.destinationViewController as! DetailViewController
var detailImages: Array<UIImage> = []
detailImages.append(UIImage(named: "pup.jpg")!)
detailImages.append(UIImage(named: "dog.png")!)
// Set the property to the selected location so when the view for
// detail view controller loads, it can access that property to get the feeditem obj
detailVC.selectedLocation = _selectedLocation;
println(_str!)
detailVC.labelText = "hello"
}
and in viewDidLoad of DetailViewController
override func viewDidLoad() {
super.viewDidLoad()
self.myLabel.text = labelText
// Do any additional setup after loading the view.
}
Related Topics
How to Horizontally Center Content of Horizontal Scrollview
How to Delay a Return-Statement in Swift
Why Does User Defaults Publisher Trigger Multiple Times
Swift Initialization Rule Confusion
How to Convert an Array to List in Realm
How to Calculate The Camera Position from Vuforia Gl Matrix
Dynamic Links Firebase Function Not Being Called at All Xcode 12
Rlmexception', Reason: 'Attempting to Modify Object Outside of a Write Transaction
Parse Migration Error to Mlabs and Heroku
Swift Error "Static Member Cannot Be Used on Instance of Type"
Why Do I Get Rgb Values of (0,0,0) for an Nsimage with a Transparent Background
Reduce Float Precision Using Regexp in Swift
Print Not Working in Swift 3 Extensions
No Trailing Closures Support for Methods with Default Parameter Values
Swift String Indexing Combines "\R\N" as One Char Instead of Two
Writing an Enum Case Check to a Bool Variable Without Equatable Conformance