Is it possible to assign multiple custom classes to the same UI Element in Swift?
I have a UITextField that I need to animate with Spring(Animation library) and I also need to use some other library on the same UITextField that provides some other functionality. I usually assign the custom class in the identity inspector custom class section in the storyboard but I can't find a way to assign multiple classes. I also tried to create a new class and inherit from both of these libraries that I am trying to use but I don't think you can have multiple inheritances in Swift.
You can't, Swift does not support multiple-inheritance. An object can implement multiple protocols but only inherit from one class.
For sorry this is not available in Swift ( Great C++ has it ) , so you have to select anyone and go with it
Ok, I just came up with a great idea that fits my needs perfectly but it might not apply to everyone. Since one of the libraries I was trying to use was purely for animation, I placed my UITextView inside a view, inherited the animation library for the view and inherited the other library for UITextView. Now I have both the animations working and the functionality.
Related
I am specifically using XIB as my UIView, and then a UIViewController for the controller in between the (M)odel and (V)iew in MVC design pattern, as recommended by Apple..
Should I just connect the first responder outlet to the button outlet in Interface Builder or should I programmatically add it to the UIViewController-based Swift class which controls that XIB view?
I guess business logic should go in the UIViewController (C)ontroller class... but what about regular application logic? I guess add a target to a button would be considered logic.. but '(V)iew' logic...
For specificity --> MODEL contains the business, application and data structure logic.
The Controller can utilize internal functions, classes, structs as well as external 'service' or 'manager' classes to manipulate the MODEL, which the view should have connected it to it, and thus updates from. Bingo :)
I actually prefer to make the code targets inside XIB classes and make an output interface for the XIB then let the ViewController or whatever view conform to It. This help Make the reusable XIB much easier to use
If we depend on The Parent View to add the target methods, so every parent will use the XIB should make Its own method, and the Parent does not have the usage guidelines (Interface), So It would lead to Inconsistency and debugging difficulty
I have UIlabel and includes few customisation of it . and this label is used in so many screens in my application. i tried with both subclassing and categories in prototype.
which is preferred one to use in this case. subclassing or categories.
Scenario - 1 : If you want to change something which happens as part of framework calls during the lifecycle of a UI object? - use subclass.
Scenario - 2 : If you want something application wide, something which is in addition to the existing functionality, and you don't care if this becomes available to all instances of this pre-existing instances of the framework class?
- use categories.
You should use Subclassing instead of category. Category should be used when you want to alter the behavior of every instance of class while Subclassing should be used when you want to alter behavior of specific instance or objects.
Category is harmful also in some cases as you are manipulation existing class so it is better to use Subclassing where possible!!
in my project I have a big amount of IBOutlet and var inside one swift file. Do u have any suggestions how to organise well this list? or categorizing in some ways? thanks
I think you can use tag to organize it or sub-class these components, add some properties as type, category, ... to handle it easier. Anyway, please carefully when you use tag.
I can just advise you to add //MARK: explain the section content to let other dev understand your organization
It depends on the content of this swift file, but usual it's a code smell.
Try to differentiate and separate variables and functions by their responsibilities.
If you have tons of IBOutlets in a single file, that could mean that possibly you are making a bad code in general. Try to divide and organize UI elements on custom subviews. For example, if you are making a chat view controller, you can split one big view to two subviews named ChatView and NewMessageView. The first one will contain and manage messages that was sent. Another one will provide a text field with "send" button and another elements like a button to upload a photo, etc.
You are free to make IBOutlets in UIView, not only in UIViewController.
I am working with a storyboard, and I have some pages on the app that have content. Instead of manually creating styles for headings, and texts by using the right side of the screen little color/font editor, is there a way to just create a single style and use it throughout those content pages?
Thanks!
Since you only want the styles to affect some of your labels, I would suggest creating a subclass of UILabel for each different style you want. In each subclass, implement the awakefromNib method to set the style properties of the label. Then, for each label you want to have the style applied to, set the class in interface builder to be your subclass
Yes there is, but it would require you to use a stylesheet framework.
For example, three20 has a TTStyle framework, which is really good at creating style classes that you can apply to labels, buttons, and views.
http://three20.info/
Check out this tutorial:
http://www.mattvague.com/three20-stylesheets-tutorial
I believe Nimbus Kit also has a style library, but I have not explored it as much:
http://nimbuskit.info/
I want to create a custom UIProperty Control that is not more than a UILabel and a UITextField grouped together and add it to IB's Custom Objects list to reuse in other Nibs.
I found a lot of documentation and everything points to creating a Xcode or IB plugin project, but everything refers to Xcode 3.x and there are no parallels in many spots.
Anyone cares to help?
Thank you in advance
Unfortunately, it's not easy, if even possible at all. Unlike Xcode 3, in XCode 4, Apple has pretty much eliminated this feature. They now say you must create the class manually first and then associate the controller object using Interface Builder.
InterfaceBuilder Docs explain how to do it now in Xcode 4 using a custom object.
And from the xcode 4 transition guide:
After adding the source file templates to your project, drag a custom view object from the library into the nib file. After adding the custom view to your nib, select the custom view and assign the correct class to it in the class field under Custom Class in the Identity inspector
Some more resources:
iphone-creating-custom-objects-for-interface-builder
Interface Builder Help - Custom Object
Xcode 4 doesn't do plugins.
Creating something that will be customizable in IB doesn't seem to be possible at this point. I think the strategy being used by and large is to avoid IB and do it all in code (e.g. most everything on cocoacontrols.com).