UINavigationController Adding Rows Implementation - iphone

I was wondering if with a UINavigationController, can you have a "+" sign, like the Contacts app, that adds a row to the main root view and has a default name as "Setup", then you can click on that row to go to one level below, change a value in a UIPickerview in the one level below UIViewController, and then press the back button and have that value from the UIPickerView be the name of the new row that was created?

Yes (though it's not very Apple-like UI behavior!)
You'd have to put some special logic in your table view data source to add the "Setup" row and go to the picker when that row was selected. The root view controller should implement the delegate from the picker to receive the new row's name, then call [self.tableView reloadData] to update the view.
A better practice would be to model the Contacts app and let the "+" button present a modal view controller from the bottom for your picker view. Look at the addButton implementation in SQLiteBooks or CoreDataBooks.

Related

How to add an "Add Button"- Button

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/

Edit and add button for UITableView in ViewController

I have a little problem, I want to add a function to two BarItems in my UINavigationBar, which is placed above the UITableView in a normal ViewController. I'm very new to programming so can you please give me the complete code that I need? The functions I want to have are:
An edit button so I can delete and change the data of a cell (title + subtitle)
An add button so I can add a new cell to my UITableView. Maybe also a function that points to another ViewController where I can type the data for Title and subtitle to save it afterwards.
Thanks!
For editing record:
You cannot get the cell that your planning to edit using button on UINavigationController, the buttons should be placed at the same cell on your UITableview to edit that specific cell. and in this way you need custom UITableviewcell.
For creating new record :
First link the button to your UIViewController class as Action.
You can append the array that your using to fill the UITableview and then call your UITableview like to refresh the data:
self.tableview.reloadData()
For presenting another UIViewController:
First drag new ViewController in your storyboard and link the button on the first ViewController into your new UIViewController and choose segue as "Show".
To save your data its a big topic you have either NSUserDefaults or CoreData so Google it.
Note : linking means by pressing ctrl-drag with mouse to your target.
Update :
Download your sample from here : https://yadi.sk/d/EJf610XWhkxDT

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.

UIDatePicker and previous view

As with the settings page, I have a UI that displays a list of parameters, one of which is a date. When the user presses the row containing the date, a controller displaying a UIDatePicker is pushed.
What is the best way to update the table row with the selected value of the UIDatePicker when the user presses the back button to go back to the list of parameters.
How about: have the picker's controller tell the table view's controller about the update (assuming that they're not already the same object), and have that controller call reloadRowsAtIndexPaths:withRowAnimation: on the table view itself.
Or is your question, how do you get the controllers to talk to each other?

Navigation view controller question

Below is what I want to implement:
The main screen of my app is a UITableView. Each row in the table view is a category, when you click the detail disclosure button in the row, you can see a bunch of items under this category in the category detail view.
Now in the main screen, I click the "+" button in navigation bar to create a new category. (The new category will become a new row in the table view). The app then take me to the "Add Category" view. (I used presentModalViewController)
In the "Add Category" view, I set something, then click "Save" button to dismiss the "Add Category" view. (I used dismissModalViewControllerAnimated)
Usually after I click "Save" button, the app will take me back to the main view and I will see a new row in the table.
But that's not what I want to go, what I want is - after I click the "save" button, the "Add category" view will be dismissed but not return to the main view. Instead, I will see the details of the new-created category so I can continue to add items under this category. The result is just like "I return to the main view and then click the detail disclosure button of the new-created row (category)".
Does any one know how to realize that? Thanks!
You can do this by doing a little decoupling of one screen from another:
- Create a custom delegate and protocol for your category creation modal dialog. Something simple like:
#protocol CategoryCreationProtocol
- (void) categoryAddDone:(NSString *)category;
- (void) categoryAddCancelled;
#end
...
#interface CategoryCreationDialog : UIViewController {
...
id<NSObject, CategoryCreationProtocol> categoryDelegate;
}
#property (assign) id< CategoryCreationProtocol, NSObject> categoryDelegate;
- In the modal dialog when the user taps the 'Save' button, after dismissing the view controller, you invoke the delegate method:
if (categoryDelegate && [categoryDelegate
respondsToSelector:#selector(categoryAddDone:)])
[categoryDelegate categoryAddDone:newCategory];
Also, something similar for the Cancel button.
- Your main controller implements the categoryAddDone method and sets itself to be the categoryDelegate for the modal dialog.
- At runtime, when the user taps Save the delegate method is invoked so your main view is notified that something has happened and it can push the right view into place and even jump to the proper category.
- In your case, as soon as the category creation is done, the main view controller is notified, so it can release the category creation dialog and push the category detail view into the stack. The user sees the modal dialog disappear and slides right into the detail view.
- In general, using delegate/protocols for push navcontroller and modal dialogs is a really handy pattern for making decoupled and reusable views. This way they can be invoked from a variety of places. To make it consistent, you may also want to have a show method on each modal dialogs and pushed view controllers that the caller can invoke. This way there's a consistent way to get into and a consistent way to get notified that the user is done with it.
If you use presentModalViewController and its corresponding dismissModalViewControllerAnimated, then you will return to the view controller in which you issued the initial presentModalViewController message.
Instead, you may want to push on the stack the view controller in charge of adding the new category, and when you are done, you simply push on the stack the view controller responsible for showing all of the items of that category. Thus, you should use
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated