How to add an "Add Button"- Button - swift

I'm looking to make a button which creates new buttons.
So there are already 3 buttons created by me with ViewController.
And on the bottom should be a "create new button" button. So the user can create as many buttons as he wishes. And every new button should be stacked under each other.
https://imgur.com/ttDZQU1
Here is how I build it on ViewController.

You have some ways to achieving this result. You can create a stack view and push the buttons to it. You can programatically add buttons to the screen by creating new UIButtons every time the users click the add button, or you can create a table view, and add a new cell every time the user clicks it. This last approach already supports scrolling (For when your user clicks tens of times, you'll need to able to scroll the screen so they can see the buttons outside the visible frame). In case you choose to go with the first or second, don't forget to add an UIScrollView to your View.
The steps to get this result with a tableview (3rd approach are):
Create a UITableView in the view controller's storyboard.
Set the UITableView datasource and delegate to the view controller
Create a UITableViewCell inside the UITableView, and set its identifier to something related to cell, like "buttonCell", for example.
Add a UIButton to this newly created UITableViewCell, and pin the constraints accordingly.
in the View Controller .swift file, create an outlet for the UITableView
in the tableView delegate cellForRowAt, don't forget to use the right identifier (in our example, "buttonCell").
if you get lost somewhere in the steps listed above, check some tutorials to create a tableview, such as: https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/

Related

Swift Add custom cell on button tap

I have a table view with one custom cell that I have already built and hooked up. When the user taps the add cell button I want it to add a different custom cell. I don't know where to start this process. Do I build it on the storyboard then hide it? Or what is the best means?
You should create all of your custom cells in the storyboard, and then implement UITableViewDataSource methods, where you return whatever cells you want.
Check out the documentation

iOS Storyboard How to access the controls and add event handlers, and binding data to controls added on Storyboard

I used to create the UI thru code. But now I have to use storyboard. I am confused about how to add the event handlers to the controls added on the storyboard and how to bind the data dynamically to the controls added on storyboard. A sample scenario is An UIView is added on the storyboard and two UITableViews and a button are added on top of it. I want to add event handler to the button and bind data to the table views. How do I do this. If I subclass the UIView added on the storyboard will I have access to the controls(button, two table views) added on top of the view or how else I should achieve this ?. Please help !
Adding an event handler to a button is relatively simple. In your UIViewController subclass simply add a method similar to the one below, then in your interface builder select the viewcontroller and on the right side panel click the right most button at the top which looks like an arrow pointing to the right. under received actions drag the circle to the button that you want to perform the action.
-(IBAction)doSomething:(id)sender{
//code for doing what you want your button to do.
}
A separate way you could do it, if you still want to do it programatically is to do the same thing you're used to doing, except in your .h file add IBOutlet UIButton *buttonName; and in the right pane under outlets you'll see your button. which can then be referenced by name within the .m file.

Static table view - disclosure indicator to call phone number

I have a table view set to as static. it has 4 cells in it. the first two cells are mapped to another view so that works fine,
I would like the other 2 cells to either call a phone number or launch email. I do have the code for both of these functions already. I just dont know who to map the action of a cell selection to a function in the code. when I right click on the cell, I only see push action for segue but I want to control this in the code.
Open the storyboard file and click on the the Assistant Editor button in the top right of Xcode.
It should open up the header file to the view controller associated with the view. Now Ctrl and drag the cell you want to hook your code up to and drag it to the header file. This will create an IBAction for you. Now that it is hooked up, you can then call your method your created to handle the call or email.

how to manage a stack of UITableViews without a navigation controller

I am new to iPhone development, and I am working on modifications to an existing iPhone app. The high-level overview of the issue is this.
I have a button displaying a pop-up containing a UITableView.
I'd like to click on a menu item and display a second UITableView with sub-items, including a back option. If the user clicks back, they go the original UITableView. If the sub-item has additional sub-items underneath it, it should (when clicked) launch another UITableView with these options. There is also a back button as a menu item that will allow the user to navigate to the previous menu displayed.
The challenge here is that I am not using a navigation controller. the original developer only wants to add UITable Views to the stack, add transitions between them as you go from one menu to the other. Most of the tutorials I have seen and tried utilize a navigation controller and Interface Builder to associate the UITableViews.
Right now, I have an XML data source populating the menu, and when I click on a menu item, the titles change correctly, but still uses the same UITableView to display the options - this has consequences of course, as some of the sub-items may not fit on a screen.
any thoughts on how this can be done? I can post some code if necessary, although I think the general description should be able to ring a bell with one of you smart guys!
This can be done in numerous ways.
I haven't done this first one, but you can probably create a UINavigationController and set its view to the appropriate frame (inside the bubble) hide the navigation bar and set the action of your back button to pop the current view controller.
Another method is to have multiple tableviews on one controller, the delegate and datasource methods have the UITableView as an argument so you can distinguish them when setting the height of your rows, headers etc and when returning a UITableViewCell.
The way I've chosen to deal with such configurations is to have one UITableView and have only the datasource change. When you reload, insert, delete rows or reload the whole table, you can change anything you want depending on the current datasource level. The easiest none animated way is to reload the whole table.
a) If your "options" go off-screen height wise (you want fixed height) table change the - (CGFloat)tableview:(UITableView *)table heightForRowAtSection:(NSInteger)section return value
b) If your "options" go off-screen length wise either make your cell's default textLabel flexible: cell.textLabel.adjustsFontSizeToFitWidth = YES; cell.textLabel.minimumFontSize = 14; or have custom cells (lookup subclassing UITableViewCell, which is recommended) for each datasource level.
If you subclass TableViewCells remember to have different dequeue cell identifiers for each level, so the table doesn't provide you with another level's cell class.
For the "stack" of tableviews or datasources, you can have an NSMutableArray with addObject for push and removeLastObject for pop.
For animations, UITableViews can animate their rows/sections for 3. (see documentation for insert, delete, reload - Rows/Sections UITableView class reference), for 2. you can even have UIView (if not CoreAnimation as Grimless suggested) animations, that move the current tableview to the left (setFrame:) and the next tableview from the right (setFrame offscreen before animation and then in place in the beginAnimation-commitAnimation clause), but make sure you add the tableviews in a container view (the bubble interior) that clips its subviews.
Oi. This is gonna be a tough one. My suggestion would be to maintain your own stack implementation. Then, use CoreAnimation to add/remove UITableViews from your main view controller to get animated effects. So whenever the user clicks on an element in the current table view, the appropriate controller creates a new controller and table view, and then your custom navigation controller pushes the old one onto the stack, removes the old table view from the main view, sets the new controller as the current one, and adds the new table view to the main view. Kinda messy, but I think it will work.

Having UITableView edit button outside of the navigation bar

I have a UIViewController, and within that view i have UITableView added in IB
The UITableView displays the items from my array beautifully
I would like to be able to edit the items i.e delete them
BUT The UITableView does not have a navigation bar, and i am not using a navigation controller within this app i am just adding and removing views manually.
What i would like to do is place an "edit" button somewhere else within the view ... is this possible? and how might i go about this?
Put a button somewhere. In an action connected to it set TableView's editing property to YES - it should work fine. You also need to implement delegate's editingStyleForRowAtIndexPath method (return UITableViewCellEditingStyleDelete to allow to delete cells).
You could make one special cell (e.g. 1st row, 1st group) a button by implementing a adaequate didSelectRowAtIndexPath.
Or you could put buttons for editing/deleting in each cell (if single deletion/editing makes sense).
Or you could place the UIIableView on a super view wich also contains the button(s) as sub views.