So I have seen a few tutorials out there on how to do this but really they all involve hard-coding buttons and text fields in. What I have done is created a view controller A, with an embedded table view. And in the navigation bar I have added a button (the "Add") plus symbol button. This button I have made its action to modally bring up another view controller B where I have a text field and a button Save.
Here is a picture for reference:
So what I want to do, is have the user be able to press the green plus button in view controller A, and then it will bring up view controller B where I can enter a username, press save, and it will dismiss itself (I know how to do this) and reveal A again but now showing the newly added contact.
I am using an NSMutableArray to store and populate the table. What is the simplest, and easiest way in which I can do this, without having to hardcode buttons in as shown in the apple docs Table View Programming Guide. I have two separate classes for A I have the Contacts class and for B I have designated it the addContact class. Both classes are simply view controller subclasses.
First I would push the second view instead of presenting it modally.
To the Segue (connector between first and second view) you can also attach an identifier.
https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles-storyboard/StoryboardSegue.html
Additionally you can perform some action in the prepareForSegue method before the view is pushed.
In this method I would then pass the array and tableview, such that you can add the username and reload the table when the save button is pressed.
http://nscookbook.com/2013/01/ios-programming-recipe-5-passing-values-between-segues-with-prepareforsegue/
The scope of your question isn't very clear, but assuming you know how to pass the saved value back to the parent view controller, you would insert the table row using something like this:
- (void)addItem:(id)item { // where item is the object being added to the data model
[self.items addObject:item]; // where self.items is your mutable array
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.items.count - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
The insertRowsAtIndexPaths:withRowAnimation: method informs the table view that you've added new items to your data model at the given index paths and it will then call your data source methods, including cellForRowAtIndexPath, to generate the new cell and animate it into place.
Related
I have this scenario:
I have a navigation controller within a view controller with table view and varius cells.
When cell is tapped, I go to another view controller (with "TEST" label text for testing).
My problem is this:
My app can be able to open from URL and in its query string there is a param that indicate which controller have to open (that with the table view or that with label).
I cant to find a pattern for to achive this in "clean" way.
For example:
I receive an URL with query param that indicate that I have to open the controller with label.
How can I organize the code to indicate that you must first go to the controller with the table and simulate the cell tap then go to the final controller?
Every navigation controller has array of view controllers. In case that you need to add both view controllers you can use next solution:
NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
[controllers addObject:tableVC];
[controllers addObject:labelVC];
[self.navigationController setViewControllers:controllers animated:YES];
This will animate labelVC without the tableVc becoming visible in the process. When the user press the back button, he will be returned to the tableVC
I have 3 view controllers
The first view controller is storing objects in an NSMutableArray from text fields, info entered by user. There are 4 fields which are being stored as one object in an NsMutableArray.
After storing the data in the array, it is then being stored in another array, array2, which is located in the 2nd view controller, the 2nd view controller has a UItableview and custom tableview cell. The array2 is displaying the information in the tableview cell.
I have an update btn which goes to the 3rd view controller and displays all the information in the tableview cell in 4 uitextfields just like in the 1st view controller. After I change the data, I am storing it again in another array, array3 which I want to pass back to the previous 2nd view controller to display the updated info in the table view cell.
Everything is working fine up till now, my question is, how can I pass the array3 back to the previous view controller to show the updated info? I am using delegates to pass data but I cannot seem to pass it back to the previous view controller. the app crashes and the error is "object cannot be nil"
I have checked various tutorials and questions but cannot find the answer. Please help.
Using delegates and arrays
You have to pass array in your delegate method so you can get this array in second viewcontroller. For example you have called delegate method like :
[self.delegate setData:(NSMutableArray *)array];
You can get this array where you define setData method like
(void)setData:(NSMutableArray *)array {
NSLog(#"Array value %#",array)
}
My suggestion is you can maintain your NSMutableArray in Appdelgate OR
Using the NSUserdefaults
-By using these you can maintain your data anywhare in the project
In my project I have two view controllers one that lists all the core data stored in a table view and a second that allows the user to input data to be stored. However when I click the back button on the navigation controller on the second it fails to refresh the tableView.
I have tried [self.tableView reloadData] in the viewDidLoad as a bit of a long shot but no joy.
How would i go about refreshing the data when the user clicks back?
Many thanks
Danny
The shortest route would be to move [self.tableView reloadData] from viewDidLoad to viewDidAppear but I would suggest using a delegate or callback so that it only reloads when necessary.
I'm brand new when it comes to app development so this might be a stupid question.
So i have made a UI table. It is customizable, as in users can insert or delete rows. I want to allow users to click on a table cell and it'll direct them to another view controller. All the view controllers will look the same for each cell (sorta like a template). Any idea how to implement this using storyboard?
Appreciate it!
You do not need an array of view controllers. All you need is one view controller, which gets instantiated when the user clicks the cell to navigate to it, and gets deallocated as soon as the user closes the screen to go back to your main view controller.
All you need to implement this in your storyboard is adding a push segue from a cell or a button in your main view controller to your "detail" view controller. When the segue gets triggerred, your code gets a chance to configure the newly created "detail" view controller in the prepareForSegue:sender: method, before the controller's view appears on the screen. This is the place where you customize the data that shows up in the detail view (presumably, depending on the particular row in the table that has triggered the segue).
Here is a link to a good tutorial explaining how to build a master-detail application with Xcode and storyboards.
In storyboard you create a viewcontroller that will display the data after a cell has been selected, you will only need one and not an array. Link it from the tableviewcontroller to the new viewcontroller. Click the segue in Xcode and in the inspector give it a unique identifier.
tableView:didSelectRowAtIndexPath: will get called when you select a cell, here you can perform the segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedObject = ... // store the object that was selected
[self performSegueWithIdentifier:#"mySegue" sender:self];
}
In your tableviewcontroller you make sure you implement prepareForSegue:sender:. Here you can hand over the correct model object to populate your destination viewcontroller with data.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"mySegue"])
{
MyDetailViewCotroller *controller = [segue destinationViewController];
controller.dataObject = self.selectedObject;
}
}
Check out this example code from Apple (does not used Storyboard though): http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007416
I have an UIView with embedded an UITableView.
One text field in the same view should control the table's cells number.
If the user enters the number 4, immediately the TableView should change its cells number to reflect what the user entered.
The content of the cells will just be something like "Please select".
Then, when the user selects a cell, a Date Picker should come up, let the user choose the date/time and then, when the picker is dismissed, that cell should change to the date/time selected.
I'm going crazy with this since more than two days. I don't put any code here because I really have no idea where to start and all the things I've tried so far were a complete failure.
If anyone can lead me to the solution, I would really appreciate it.
Thanks.
If you're on the iPad, you could show a UIPopOverController from the selected cell when the user selects it. On an iPhone, you could present a modal view controller.
For this, see -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.
You'll have to implement code that handles getting the value from the UIDatePicker when its presenting view controller is dismissed. Also, you'll have to host the UIDatePicker in a view controller, as there is no way (as far as I know) to present it on its own.
So, my proposed solution is:
Create a new view controller that hosts the UIDatePicker you want to present.
On that view controller, put "OK" and "Cancel" buttons.
Add a property to that view controller that references your "main" view controller. This is so that the view controller that hosts the UIDatePicker can send a message to the "main" view controller when you're done selecting the date.
In your "main" view controller, handle the tableView:didSelectRowAtIndexPath: event for your UITableView.
Present the view controller to show the UIDatePicker to the user.
If the user selects "OK", dismiss the modal/popOver view controller and update the cell with the new information (you'll probably need an array of NSDate values and a call to [tableView reloadData] to perform this).
If the user selects "Cancel", dismiss the modal/popOver view controller and DON'T UPDATE ANYTHING.
i think this tutorial will help you.. i have followed that too..i was also faced the same problem that you have..