How to add UILabel to UICollectionView Controller [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 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.

Related

How to not repeat code in Swift for attributes of button? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to IOS Development. I am creating an app with a lot of buttons. How do I give the same attributes to all the buttons with help of a class?
Your answer really isn't ideal for stackoverflow since it's not a direct problem your looking for help with, but instead a question of best practice. Check the code of conduct again.
However you can do the following:
(If you need those buttons to be instanced/complete right away)
a) Create a private static function in your class which builds your buttons with the attributes, and gives them the text and selector you pass via parameters.
b) Create a private static function which only applies the attributes you require
(If you just want to apply the same style)
a) Create a private function in your class which stylises all buttons upon viewDidLoad
There are a few things you could try:
1 - You could try and create a subclass of UIButton with the common styling code, and use that custom button in your program instead of UIButton.
2 - You can extend UIButton to have a styling category function, then call that function from viewDidLoad() in your view controller or awakeFromNib() in your view.
3 - You could use the appearance protocol to style some aspects of the button (though it can be a bit limiting).
The approach you choose to do and how you choose to develop is something you need to decide as a developer.

Cleaning of ImageView [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 5 years ago.
Improve this question
I need to clean ImageView. I have some figures there.
#IBOutlet weak var imageView: UIImageView!
I tried using
imageView.image = nil
but it doesn't work. How can I clean imageView?
ARC counts the strong references to an object. If there is none, that object is deallocated.
If your the image view isn't strongly referenced from anywhere else, then removing it from the view hierarchy should do the job:
imageView.removeFromSuperview()

Why is this error happening with prepareForSegue in Swift? [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 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.

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