How do I add functionality to a UI made in storyboard? - iphone

I have made a pretty little UI on storyboard but I'm quite new to XCode and Objective C (coming over from Java) and I'm having some trouble adding functionality. I know that I should make a subclass of UITableViewController (storeTableViewController) and set the class of the storyboard controller to that. The problem is that when I do that all my hard work in the storyboard is erased, and I can see where the code is conflicting with the showing of my UI (for instance, the tableView:numberOfRowsInSection returns 0 always), and I think that with enough time and patience I could program the UI, but is there any way that I can use all this work that I've done on the storyboard and still be able to add functionality? I want to set a text box at the top to be the first responder as well as be able to change an image and labels on command programmatically.

You should probably use IBOutlets. Connect the StoryBoard UI Object to IBOutlets and you can modify the properties of the UI objects from the code
You can declare IBOutlet link this
IBOutlet type variableName
Check this tutorial video which will help you a lot.
Video Link

I use the storyboard as a canvas. I plug in the outlets that I want and all the little details and then I go around and built the classes. That being said, you need to declare a urtext field outlet (depending to what you want it to do) then add the delegates necessary and allow it to perform on selector. Then you can declare the function. Linking it in storyboard is easy by control and drag.as far the image you can use an uiimage and declare its function and how it changes depending to what the changes are the code varies. My suggestion is to take screen shot of your storyboard and post it with your question so I/ we can help you with the code. But as far as your question goes I hope this answer satisfies you. Happy coding.:)

Related

Swift #IBActions in ViewController Extension

I’ve noticed in Swift/Xcode 11 that you can’t ctrl-drag an IB object into a ViewController extension to create an IBAction, but you can cut/paste it into the ext. after ctrl-dragging to create the IBAction in the ViewController class & it works. Anything wrong w/moving an IBAction to a ViewController extension using copy and paste? And why is the Ctrl drag not working for the VC extension? I was putting UITextView actions in an ext w/other TextView funcs ‬when I noticed this but don’t fully understand why the ctrl-drag won’t work but cut/paste appears to show no prob. Thx for considering the Q.
Unless someone works for Apple I'm not sure we'll get an actual answer here. I can guess and give you an alternative that worked for me though.
From an Xcode feature implementation standpoint, the code that supports these actions probably varies..
From what I've seen control drag works when the class of the VC on the storyboard directly matches the class of the object you're dragging into.
I've never tried dragging into extensions but I can see how that might be complicated given that stored properties are not allowed on extensions which adds complications given you can drag outlets too.
However, I've also done IBAction/IBOutlet's on parent classes and I was able to get that to work when I started dragging FROM the an existing IBAction func (there's a circle to the left of it in the code) TO the storyboard of the child class.
In short, I'm assuming there's a lot of assumptions and code being done to allow you to drag from storyboard onto code, and I'm pretty sure limitations are due to the complications in handling the corner cases. Apple will often opt towards not allowing behavior at all if the possibility for a broken corner case exists. Some of the other ways of creating these connections (like a copy and paste) are prob simpler.

When I add tableview to the ViewController,tableview is not added correctly

The output of this tableview not appearing correctly.what is the reason to come like that
So the tableView that you just drag into a view will have default properties associated with it. When you run it on a phone/sim it doesn't actually have the 'Table View' heading (that's just there to make it easier to design the storyboard), it'll have a white background with no rows and will look like there's nothing on the screen. Furthermore it may not even be at the location you placed it at depending on how the constraints default when rendering the scene.
It seems like you have a little to learn about how this works. When creating a new storyboard, generally speaking you associate a ViewController class to it. Assuming you have dynamic data to display on the table, you connect references to the tableView to the class, then you need to assign a delegate to the table and then implement the protocols. Then if you have custom cells, it adds a little more to do. Theres several great tutorials from Apple itself on how to do all this in swift.
If you just want to see how the table would look; in the storyboard itself you can create prototype cells in the table and design it out that way.
Either way, make sure you read about constraints and how to make it look right when rendering on a phone. Just dragging things onto the story board does not mean that when you finally run it, it will look exactly as you designed it. You need to specify how components align relative to each other, which is essentially what constraints do.

Material TextView via Storyboard?

Is there a way to implement the material TextView via the storyboard? The example only shows via programmatically. Please help?
The answer is actually quite simple, and I just discovered it myself while attempting to do what you asked. All you need to do is go to the identity inspector of whatever object you want to use as a material object, then in the "class" field, put the ID of whatever material class you're using (i.e. "RaisedButton"), and you're all set! Hope this helps!
The tricky part about using a TextView in storyboards with the Material Text class that does pattern detection is the instantiation part. Storyboards take care of the initialization for you, which makes it difficult to update the textContainer layer, as it needs to be passed to the TextView when initialized. I am looking into this to see if there is a solution.
If you look at the source of TextField, you will see that it is a subclass of UITextField. This means that you can just change the class of a UITextField object that you drop onto your Storyboard, and then do any other configuration in code.

Altering characteristics of 3-party buttons (!) in Xcode

I'm doing an iPad app in Xcode 4.3
So, I am triggering a function and inside that function, I want to be able to alter the characteristics of other buttons. For example, upon clicking my button (that I made in Interface Builder) linked to the function, I want to have a load of other buttons (also in IB) change text/color.
I've tried the developer book and a good amount of time on google, but I can't find the question asked or answered in the way i'm trying to phrase it (I decided this was the most clear one in the end)
I know I can easily edit the name of the button I've used to call the function
[MYBUTTON setTitle:(NSString *) TITLE forState:(UIControlState) STATE];
but I can't figure out how on earth to edit other buttons I've added in the interface builder. Is this possible, or do I need to deal with the buttons more programmatically?
so far in my function, I've called the IBAction and made a load of tags to relate to the buttons that I want to change - am I heading along the right road?
Hope this is clear about what I'm trying to achieve, and be gentle with me, my objective C isn't that good yet!
You can mark button properties or instance variables with the IBOutlet marker and then hook the buttons in Interface Builder up to your object.
Also, it isn't a good idea to have all capital letter variable names. In C convention, those are usually reserved for preprocessor macros.
create IBOutlet properties for each button in your view controller, link them in IB or Storyboard and finally change their properties in your method :)

Does storyboard eliminate the need for .nib

With the introduction of storyboard, I wouldn't select to create a .xib/.nib when I create a subclass of UIViewController, because I can just drag out a viewcontroller in interface builder and assign it to the new class.
So, with storyboard, when would i need to use the .xib/.nib file?
Thank you.
Storyboards don't completely remove the need for NIB files. One common example is when creating subviews to go inside a UIScrollView. You can't create the subviews separately in a storyboard; instead you have to create separate NIB(s) for the subviews and attach them to the scroll view programatically.
In fact, on almost any occasion where you have a need for subviews which change at runtime you'll want to use separate NIBs.
Storyboards are great for certain kinds of apps, but for anything even remotely complicated the old way works very well still. An additional benefit is that they co-exist nicely. You can put placeholder views in your storyboard and fill them programatically with NIB-defined views later on.
I guess, storyboard contains the .xib/.nib files for all your views. It present relationships between them and helps you to avoid confusion if you have a LOT of views. Also it saves your time and nerves when writing code.
I have tried out storyboarding lately and I do have mixed feelings about its use:
Pro's
Convenience: You might have to write much less code. So you can for example show a static view without having to write any code.
Overview: You can quickly see how the ViewControllers relate to each other
Efficiency: For the simple use cases I find the Storyboarding much more efficient than "the old way".
Example 1: Editor > Embed In > Navigation Controller and voilà, no more instantiating and configuring is required.
Example 2: You can make "Prototype Cells" for TableView which speeds up the creation of static table views dramatically. AFAIK this is not possible with nib files
It's cool :-)
Con's
I find that the re-usability somehow suffers with storyboards. Although I can segue to a given ViewController from multiple points, it is still limited since in one case I might use the VC as a Popover and in another as a Master-ViewController.
Assuming that you are working in a big team and different people are changing different parts of your application, having one monolithic storyboard which you then have to merge might pose a big problem team-work-wise.
I really would like to have some mechanism to include or reference existing nib files into the storyboard which is currently not possible.
So considering these points I still find storyboarding very appealing but it makes sense to combine both approaches which is not really a big deal. Additionally, this technology it is still quite new so it might improve in the (near) future.
If you use storyboards, you generally won't need to create separate .xib files. You can still use a .xib file if you want to -- it might be useful e.g. if you have a particularly complex view that you'd like to configure by itself. Most of the time, though, there won't be a need to do that.