Differentiate between multiple UIButtons with empty titles - swift

I have a bunch of UIButtons (10-15 of them) in one of my Views. I want to display them as an image without the title text, so what I did was set the image for the Button through the storyboard, and then set an empty title for the button.
I want to react differently when each button is pressed. There are a couple of solutions that I can think of off the top of my head, and I'm wondering which would make the most sense, software engineering-wise:
Each button is connected to a different IBAction (too many methods added)
Each button is connected to a different IBOutlet (how to differentiate between the buttons, still? Use ===?)
Single IBAction, unique tags for each button (this is what I'm doing for now as it's simplest and it works, but aren't tags a bit hacky?)
Give each button a different title, but find a way to hide the titles, and then connect them all to an [UIView] outlet array (is it possible to hide the titles from the storyboard, or must I do it programmatically from the View Controller?)

Some solutions you thought of are viable, some others aren't (connecting #IBOutlets and trying use the equal operator, or hiding the button's title).
I would recommend you to simply keep using UIButton tags (as that's what tags are there for) and use a single entry-point #IBAction. You will then be able to identify each button by their tag.
Subclassing your buttons might seem a bit overkill if you're only trying to identify them...

Related

How to build this screen in iOS?

I'm new to iOS development and am trying to build something like the screen below:
If I was doing it in Android, I can easily build the above UI in a few minutes. However, I don't know how to go about it with iOS.
I understand that the whole ViewController can be embedded in a navigation controller, which produces the title bar above. What about the bottom part though? I'm thinking of using something like a grouped UITableView but I'm not sure, since every cell will have very different contents:
A search bar, perhaps a subclassed UISearchBar, which I also don't know how to customize--the Search button at the right is required but isn't in the default UISearchBar. When the user taps on it, the UISearchBar must be translated to the navigation bar, no need to display a UITableView of suggested results. I don't know how to do that, too.
A button that, when tapped, flies in a modal from the bottom (I imagine it to be another ViewController with a grouped UITableView), to allow the user to choose from defined locations. Once selected, the modal closes and the button text is replaced with the selected location. This sounds much easier to do.
A header ("Item categories") and the list of categories, which may change in number. If the parent isn't a grouped UITableView, I think this part can be a UILabel and a non-scrolling UITableView with a height that changes depending on how many cells it has. If there are plenty table cells that don't fit given the screen's height, everything below the navigation bar can be scrolled vertically. That, I also don't know how to do.
If anyone can just guide me to what native iOS components I can use to build the above screen, and maybe a couple of tutorials to the things I just said I don't know how to do, I'd appreciate it.
You said it right .All the basic info you need is with you.
To build a searchbar like that i dont think you have to subclass it.
Bottom comprises of tableview.
Actually these Questions are seperately available in SO itself.So search seperately for your needs and you can achieve whatever you want
One basic principle : You cant achive anything by just thinking.Trying and get to it and if you have any issues look forward at it.All the issues will have an answer on the way.
Lots of components you need there.. Search Bar, UIPickerView and UITableView. I would like to give you some pointers.
1) You can refer http://www.appcoda.com/how-to-add-search-bar-uitableview/ for Search bar
2) When clicking on the Button, you can bring up a UIPickerView instead of another controller. For that you can refer http://www.techotopia.com/index.php/An_iOS_5_iPhone_UIPickerView_Example
3) You can use a normal Tableview with a single section, configure the section header to display Item categories.
You are asking too much to ask how to do every component of your UI. I will just answer a little.
Yes, a grouped table view is a good design. I am using a grouped table view for varying types of input. I have an actual table, with rows that can be added, and deleted, and contain an editable text area. Then I have three groups that only really show one piece of content each: two are sliders and one is a switch.
For choosing the location, pushing another view controller on your navigation stack would be the more typical way to handle it. You will save some effort that way, with some buttons and behaviors built in, but a modal view controller is not much harder. I'm not sure if you can make a navigation view fly in vertically, but does your app have to be frame-for-frame identical to the android version?

On iOS, how to make a dynamic list of "links" or buttons call a method with a string argument?

It seems that a common way to make a text behave like a link on iOS is to make it a UIButton, but I noticed the UIButton's addTarget or gestureRecognizer's addTarget both don't have an argument that can be passed to the method when the button is pressed?
The situation is, from a server, we may get back a list of words, such as "pineapple", "apple", "orange", and the numbers can vary. These words are displayed on screen, and pressing the word will invoke a ViewController to replace the main view controller.
It seems that one way is to use UIButton's tag, so when we set up the button, we give it a tag of 0, and in another array of the current view or view controller, make element 0 point to an NSString object, containing the word "pineapple". And so in the handler, the tag can be obtained, and it can be used to retrieve the string. But is this the only way, because it seems not very structural. Is there a better way?
My knee-jerk answer was to simply suggest that you subclass the UIButton. Whenever you want to add a property to an existing class, "subclass" is the first answer that comes to mind. But when I tried subclassing the UIButton, it did not work well. Searching for for "UIButton subclass" I discover that this is a well known issue with several recommended solutions:
Subclass UIButton to add a property
objective C: Buttons created from subclass of UIButton class not working
create uibutton subclass
I've tried both creating a category with associative references as well as the simplistic approach of just subclassing a UIView instead, and make the desired button a subview of that. Both approaches work fine. But the intuitively attractive option of just subclassing UIButton does not work well.
But while the various work-arounds for adding properties to UIButton objects work, they seem sufficiently unintuitive that, I'd be inclined to go back to something simple, such as using the tag numbers in an array or dictionary, rather than pursuing these cumbersome button subclassing techniques.
Create a mutable dictionary. To associate a word with a button, add the buttons address wrapped in a nsnumber as the key and the word as the object. When the button is pressed and sends the action message along with 'sender', you can retrieve the current word from the dictionary.

How to put images in action sheet button?

I have created an action sheet which contains more than one button, and I want to put different images on different buttons, how can I do that?
You can't, the "buttons" (actually a special class called a three part button, IIRC) in UIActionSheet are not directly accessible and you can't subclass it either. However it is pretty easy to write your own, see http://blog.corywiles.com/custom-uiactionsheet-using-core-animation for an example or inspiration. It's only a view with some buttons as subviews, and a bit of code to handle sliding it up from the bottom of the screen.

Add extra button to ABPersonViewController

Im working on a small app that displays contact and biography details.
You can see two screenshot here: contactDetails, biogDetails.
At the moment I have an Action button on the right hand side of the NavigationBar that displays an ActionSheet where the user can perform various actions like:
"add to favorites"
"update data",
"Biog Details", etc.
I feel the "Biog Details" is not an action as the others and I would like to display it on a different way.
I was wondering if there is a way to add an extra custom button to the PersonViewController.
I dont really want to create a lookalike of the PersonViewController, because i would lose functionality that I can replicate with the public APIs.
The other option could maybe be to have a segmentedController on the center of the NavigationBar that would switch between the two viewsControllers. How could I do that?
Im open to ideas.
Thanks,
The only way you'll be able to add that extra button is if you use the UIToolBar instead of the UINavigationBar.
If you decide to use the UISegmentedControl you'll also have to use the UIToolBar.
You can duplicate the UISegmentedControl on both views but with the appropriately selected segment on either.
Or you could use one "base" view, add the other two as subviews and hide/unhide the appropriate view depending on the segment that is selected. This would however mean that you change the position of the subviews (by adjusting their rect parameters) so that they appear just below the UIToolBar

How to realize the same effect like iPhone's homescreen

I want to add some custom buttons and realize the same effect like the iPhone's home screen. What I can think of is to calculate the position of each button and add them to the view. Are there any other ways to do this? e.g. add buttons to the tableview
Check TTLauncherView from Three20,
I realized the same view of the thumbnails in the photo app (which in principle differs only because of the background color and the rounded effect of the buttons) using a custom cell (with 4 UIButtons inside) in a normal tableview.
In my case, this is because I need to scroll up and down, in your specific case there should be a way to "lock" the table from scrolling. By the way, for this reason, it could be simpler to design the custom view in the interface builder, it is very quick to design such a view, and then create a custom controller to provide simple methods to assign icons and actions to the UIButtons dynamically.
You could also look at the Three20 libraries as already suggested, it is already implemented, but you app will easily be rejected by Apple if you do so.