Why is this error happening with prepareForSegue in Swift? [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I created a navigation controller and put 2 view controllers linked to it. One is called FirstLaunchVC and the other is FirstLaunchVC2, I want the user to put his name in the text field nameTxtField and when he clicks on the continueBtn it should lead to the other. In this second view (FirstLaunchVC2) there is a label called nameGreetings which will show the name of the user as a greeting with prepareForSegue, the thing is that it's crashing, saying that there are breakpoints on the line of the performSegueWithIdentifier and on the line where I write "nextVC.nameGreetings.text = "(str) etc etc". I have no clue why, can anyone help me with that? Btw, I've already checked the identifier and it's correct.

Is nameGreetings an IBOutlet? You cannot set the IBOutlet controls for the destination view controller in prepareForSegue of the originating view controller because while the destination view controller has been instantiated, its views and IBOutlet references have not. The prepareForSegue should feel free to update String properties of the destination, but not its IBOutlet references.
So updating of name is fine, but the nameGreetings should not be set in prepareForSegue, but rather that should be deferred until the the viewDidLoad of that FirstLaunchVC2.

Related

How to add UILabel to UICollectionView Controller [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I created a collectionView Controller in storyboards. Everything works fine , but now i need to create UILabel in CenterX/CenterY position.
#IBOutlet var routesCollectionView: UICollectionView!
messageTextLabel.isHidden = false
messageTextLabel.centerXAnchor.constraint(equalTo: self. routesCollectionView.centerXAnchor).isActive = true
messageTextLabel.centerYAnchor.constraint(equalTo: self. routesCollectionView.centerYAnchor).isActive = true
self.routesCollectionView.addSubview(messageTextLabel)
This code gives me an error. I understand that theres no view in collection view controller. Any idea how i can add this UILabel?
Always set constraints on a view after you add it as a subview.
Take a look at the console, there is probably:
Unable to install constraint on view. Does the constraint reference
something from outside the subtree of the view? That's illegal.
If that is the case, make sure to add subviews before you anchor it.

How to let a view controller know it should fetch data from the server? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to find the right way of notifying a view controller that it should fetch data from the server based on an action of another view controller.
For example - a view controller that presents a list of tweets, and a view controller that lets the user create a tweet. After the user has created a tweet, the view controller that was responsible for creating the tweet is being dismissed and the view controller that is responsible for presenting the tweet should now be aware that it should reload it's data from the server in order to present the user with the tweet he just created.
At first, I thought NSNotificationCenter is the way to go, but after doing a little research (mainly this twitter discussion), I found that it is recommended to remove the notification observer in the viewDidDisappear method of the view controller, which in the scenario described above, makes the notification useless because the presenting view controller would already remove itself from the observers of the notification by the time it should receive it (because it is being hidden by the 'create tweet' view controller, thus causing it's viewDidDisappear method getting called).
Delegation is also a problem here, because there might be other view controllers that might also be in the need of knowing when a new tweet has been created, in order to update their views / fetching data from the server / etc.
Due to the fact that the described flow is very popular, I figured there must be a correct way to let these two view controller communicate in a way that will make sense.
Any ideas?
You can set a flag in your data/network model.
Let's say you have a TweetNetworking.swift file which contains your server requests, you could add a var needFeedRefresh: BOOL.
Then it's only a matter of setting it to true when you need, and false once you've refreshed the feed.
You can then check against that variable in viewWillAppear.
Another way to go is to type this variable as an NSDate if you need periodical refreshes, and set it to NSDate(timeIntervalSince1970:0) when you need to force refresh.
If the only obstacle to use NSNotificationCenter is the fact, that observer is removed in viewDidDisapper, you can move calling removeObserver to deinit, assuming that your observer is only hidden and not deallocated.
For more information see: iOS8: Where To Remove Observer for NSNotification in Swift (it's a post written by the same author as the tweet discussion you mentioned).
You can also add notification listener in presenting view controller when you display the another view controller to compose tweet. Then you remove that listener in the callback; then the listener will be removed asap.
func showTweetComposer() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(UIPresentingViewController.tweetComposed(_:)), name: UITweetComposeViewControllerDoneKey, object: nil)
}
func tweetComposed(notification: NSNotification) {
NSNotificationCenter.defaultCenter().removeObserver(self, name:notification.name, object: nil)
}

How to assign a set of methods to UITextFieldDelegate [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to know how to fire a method when a particular text box is edited.
For example, it is explained here how to assign a certain format to a textbox, using a number of functions. However, I don't know where to copy-paste these functions. In the explanation it says to use UITextFieldDelegate, but I don't know where it is.
I have a AppDelegate class, a ViewController class (including its xib), and a UITextField in the xib. I want the code to work for this UITextField.
Add those methods in your ViewController.m class
and then in your viewDidLoad method of ViewController.m Class
[yourTextField addTarget:yourTextFieldDelegate
action:#selector(reformatAsCardNumber:)
forControlEvents:UIControlEventEditingChanged];
set reformatCardNumber: to be called whenever the text field fires a UIControlEventEditingChanged event
also add delegete in Viewcontroller.h file
and Thats Done.

Changing UIView To be instance from UIControl Programmatically [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a button that will change the view's class to UIControl
i have typed this line of code but Xcode say it is a error
-(IBAction)button:(id)sender
{
self.view.class = [UIControl class];
}
So guys my question is how to change my view's class to UIControl class programmatically
In objective C you cannot assign the class property of an object and within instance of UIViewController (which I'm assuming you're code resides in) you cannot assign the view property outside of loadView without causing issues.
I think your general question on changing an objective C's class is slightly misguided and you may need reword your question such that it's possible to suggest a way to do what you're trying to do in cocoa.
What are you actually trying to do? Are you attempting to change what's displayed on screen in response to an event? Are you trying to change the behaviour of your view controller's view somehow?
These things are usually done by adding/changing/modifying the view hierarchy of your view controller by adding other UIView instances rather than modifying existing ones.
You can define your own class that inherits form UIControl easily but there's a lot more you need to do to begin using it in the above example.
#interface CustomClass : UIControl
…
#emd
Can I suggest the following introduction to Objective C and iPhone programming guides form Apple that may shed some light on how to do things in Cocoa.
Learning Objective C - A Primer
Programming with Objective C
iOS App Programming Guide
You cant change the class type at runtime.
Alternatively you can set your control to stop handling user interactions:
self.userInteractionEnabled = NO;

navigation between views in iOS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im trying to make my first app in Xcode. This tutorial http://www.youtube.com/watch?v=rgd6mCuzlEc helped me understand how to make navigation between views. In my app I need more buttons on first and second view but buttons on second view are dependet on buttons from first view.
For example buttons on first view:
- birds
- dogs
- fishes
second view for birds:
- stork
- sparrow
- chicken
On a third view will be describtion for chosen animal.
What should be my next step? Example from link is good for my app?
You could declare your initializer for your second view controller to take data. When you initialize that view controller from your first view controller, just be sure to pass the data. Then you could set the button text in your viewDidLoad: method on your second view controller (You could try doing this in your initWithData method but sometimes xib elements aren't properly initialized at that point)
what you need to do is create a public (in the .h file) variable in the second view. lets say this variable will be NSString:
#property (nonatomic,strong) NSString *caseStr;
now dont forget to create an initialiser in your second view .m file
#synthesize caseStr = _caseStr;
-(void)setCaseStr:(NSString *)caseStr
{
_caseStr = caseStr;
}
now on your first view implement the "prepareForSegue" method:
(we assume that your segue identifier is "ChosenAnimal" and type of bird is a local string that set to be "Birds","Dogs" or "Fish"
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ChosenAnimal"]) {
[segue.destinationViewController setCaseStr:typeOfBird];
}
}
now you have the info of what been selected in the first view and you can do with it whatever you want in the second view.
BTW: if you would ever like to control the first view from the second view you will have to use delegate. good luck

Resources