how to give one viewController add and edit functionality using storyboard - ios

I am using storyboard and Xcode 5. I am new to ios.
How can i give a same view controller two functionality
User can add data and edit data as well.
I have bar button in my navigation bar.
DispalyDataController 1 : UITableView
AddEditController 2 : Textfields, label simple form data
Once AddEditController 2 is filled up then it must be displayed on tableview on DispalyDataController 1
I am using below code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AddEditController *controllerAddEdit;
controllerAddEdit.arraypassed = self.dataArray;
}

You need to set identifier by selecting an segue and move to attribute inspector
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"Edit"] ){
AddEditController *controllerAddEdit = segue.destinationViewController;
controllerAddEdit.editModeOn = YES;
controllerAddEdit.arraypassed = self.dataArray;
}
if([[segue identifier] isEqualToString:#"Add"] ){
AddEditController *controllerAddEdit = segue.destinationViewController;
controllerAddEdit.editModeOn = NO;
}
}

The problem is that your view controller is nil, you need to pull it out of the segue... Try this...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
AddEditController *controllerAddEdit = (AddEditController *)[segue destinationViewController];
controllerAddEdit.arraypassed = self.dataArray;
}

Related

'-[UINavigationController setLanguage:]: unrecognized selector sent to instance 0x7f8fba7df4e0'

A few hours ago this code gave me no problems, however, after updating my XCode
my XCode and removing a 3rd party framework, it is suddenly giving me the above exception. I'm extremely confused because I don't think anything's changed between then and now that could cause the problem besides updating my XCode. Can anyone see an issue?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*We check to make sure the segue corresponds to segue we created when a cell is selected*/
if ([[segue identifier] isEqualToString:#"show"]) {
/*Get a pointer to the selected row*/
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
/*Get a pointer to the ViewController we will segue to*/
ViewController *viewController = segue.destinationViewController;
/*Pass the dialect and code back to the ViewController*/
viewController.language = self.languages.allKeys[indexPath.row];
viewController.code = [self.languages objectForKey:viewController.language];
}
}
Specifically, the following two lines are causing the exception:
viewController.language = self.languages.allKeys[indexPath.row];
viewController.code = [self.languages objectForKey:viewController.language];
Your segue's destination controller is a UINavigationController, your view controller is likely the top view controller of that navigation controller. You probably want:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*We check to make sure the segue corresponds to segue we created when a cell is selected*/
if ([[segue identifier] isEqualToString:#"show"]) {
/*Get a pointer to the selected row*/
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *navController = segue.destinationViewController
/*Get a pointer to the ViewController we will segue to*/
ViewController *viewController = navController.topViewController;
/*Pass the dialect and code back to the ViewController*/
viewController.language = self.languages.allKeys[indexPath.row];
viewController.code = [self.languages objectForKey:viewController.language];
}
}

UIViewController not passing data between views

I'm trying to pass some data between UIViewControllers in a storyboard. I have some code below to pass the data. Whne I log the string that i'm saving on the original screen it's fine, but when I go to the UIDetailViewController the go is returning (null). What's my issue??
Original View Controller:
-(void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView==_countryTableView) {
if (indexPath.section==0) {
AthleteDataTransfer *dataTransfer = [[AthleteDataTransfer alloc] init];
dataTransfer.name=#"Yo";
dataTransfer.dob=#"01/01/2000";
dataTransfer.description=#"I am awesome";
dataTransfer.hometown=#"Launy";
NSLog(#"%#", dataTransfer.name);
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
}
}
Detail View Controller:
AthleteDataTransfer *dataTransfer = [[AthleteDataTransfer alloc] init];
_name.text=dataTransfer.name;
dataTransfer.dob=_dob.text;
dataTransfer.description=_description.text;
dataTransfer.hometown=_hometown.text;
NSLog(#"%#", dataTransfer.name);
Insted of passing data in tableViews didSelectRowAtIndexPath, pass the data in prepareForSegue method.
Like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showDetail"]) {
AthleteDataTransfer *dataTransfer = (AthleteDataTransfer *)[segue destinationViewController];
dataTransfer.name=#"Yo";
dataTransfer.dob=#"01/01/2000";
dataTransfer.description=#"I am awesome";
dataTransfer.hometown=#"Launy";
}
}
You should use prepareForSegue method to accomplish this.
and if you have more than one segues from this ViewController you can use segue.Identifier to distinguish
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if(segue.identifier==#"yoursegueidentitifer")
{
DetailedViewController *detailController = [segue destinationViewController];
detailController.nameoftextview.text =self.textViewName.text;
}
}
As suggested in other answers out here you should pass data in the prepareForSegue method. I'am not going to repeat the same code here. I wanted point out one thing that you dont want to create the DetailViewController again in the detailViewController like below
AthleteDataTransfer *dataTransfer = [[AthleteDataTransfer alloc] init]; // This is wrong and not required at all
NSLog(#"%#", dataTransfer.name);
Instead you should use self to refer the current controller
NSLog(#"%#", self.name); // This would be enough
For posting the text data to your destination View Controller, you can use the prepareForSegue method like this.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DestinationViewController *destController = [segue destinationViewController];
destController._yourTextView.text == _yourText.text;
}
}
Here, DesctinationViewController is the controller to which you want to pass the values and in the next step, you assign the values by using its object and the properties declared on that controller.
Since you are using performSegueWithIdentifier, this will automatically call prepareForSegue after performSegueWithIdentifier is executed which will show up your destination View Controller.

navigation bar back button repeated

I have a tableView with push segue to a detailView, and in this second view a button that perform a segue to a map location.
My problem is the back button of navigation bar appear twice (different icons also) in the map location view.
I guess is something of pushing twice the navigationbar, but I can't resolve it.
and this the method that I use to perform segues, to the detail view first
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue_ID"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//here I pass info between views
DetailView *destViewController = segue.destinationViewController;
}
}
and to the map:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showMap"]) {
MapViewController *vc = [segue destinationViewController];
//here I pass info between views
vc.object = self.object;
}
}
Any help? please
Have you put in LeftBarButtonItem, and also BackButtonItem for your nav bar?
Try making one of them nil.

Segue won't change screens when clicked

I am trying to create an app with multiple scenes the first two scenes connect and transition fine using a segue, but when i add a third scene and connect it with a segue nothing happens when i click the control to transition. Here is the code i have so far
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"showStops"]){
NSIndexPath *indexPath = [self.directionTableView indexPathForSelectedRow];
StopsViewController *destViewController = (StopsViewController *)segue.destinationViewController;
destViewController.routeNumber =[[feeds
objectAtIndex:indexPath.row]objectForKey:#"rtNumber" ];
destViewController.direction =[[feeds objectAtIndex:indexPath.row]objectForKey:#"Direction"];
}
}
So let me see if I got it. U have a screen that connect to two others screens right? So u need two Segues with differents identifier. Than in method prepareForSegue u will check wich one was clicked.
Something like this!
- (IBAction)firstConnection:(UIButton *)sender {
[self performSegueWithIdentifier:#"firstSegue" sender:nil];
}
- (IBAction)secondConnection:(UIButton *)sender {
[self performSegueWithIdentifier:#"secondSegue" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"firstSegue"]){
// YOUR TRANSITION
}
if([segue.identifier isEqualToString:#"secondSegue"]){
// YOUR TRANSITION
}
}

How do I set the title on the destination view controller during prepareForSegue:

I want to simply set the title on my destination view controller so that it shows in its navigation controller's navigation bar in the prepareForSegue: method however setting its title or navigationItem like so:
[segue.destinationViewController setTitle:#"doesn't work"];
[segue.destinationViewController.navigationItem setTitle:#"this either"];
doesn't work possibly because the destination's view controller's view isn't loaded yet. Can I do this without creating a custom destination view controller?
Try accessing your ViewController that is embedded in the UINavigationController like this.
First you give your segue an identifier in the interface builder, then you access the segue in the prepareForSegue method and set the title by accessing the topViewController property of the navigation controller you segue to.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"yourSegueIdentifier"]) {
UINavigationController *navController =
(UINavigationController*)[segue destinationViewController];
YourViewController *destViewController =
(YourViewController* )[navController topViewController];
destViewController.navgationItem.title.text = #"Your new title";
}
}
This is for a segue into another UITableView
Set a public NSString property in the destination UITableViewController file.
#property (strong, nonatomic) NSString *navBarTitle;
Override the setter in the .m file just to be sure.
- (void) setNavBarTitle:(NSString *)navBarTitle
{
_navBarTitle = navBarTitle;
}
In the originating tableView's segue method, pass whatever string you need in the title.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: #"userDetails"]) {
UsersActivitiesTableViewController *destinationController = segue.destinationViewController;
//Get the index path of the cell the user just clicked
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//Grab the object from the array of users if the next view requires a specific title for a user name etc.
UserMO *thisUser = [self.users objectAtIndex:indexPath.row];
//Pass the string you want as the title to the public NSString property
destinationController.navBarTitle = thisUser.name;
}
}
And now for the important bit....
In the destination controller, get the top controller of the view and set the title property, after the view is loaded, and before it has been displayed:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController topViewController].title = self.navBarTitle;
}
If you want a static title this can now (i.e., in Xcode 4.6.3) this can be done in Storyboard simply by setting the title in the nav bar on the relevant view controller.
If however you want the nav bar title to change according to what view is being segued to, e.g., a detail of a particular table row, as far as I know that needs to be set programmatically.
It took me FOREVER (newbie, sigh!) to figure out how to modify Julian Vogels' correct answer to call the key I wanted to use. Here's what worked:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"DetailSegue"]) {
// Fetch Item
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *item = [self.groceries objectAtIndex:[indexPath row]];
// Configure Detail View Controller
TPDetailViewController *vc = [segue destinationViewController];
vc.navigationItem.title = [item objectForKey:#"name"];
[vc setItem:item];
}
If the segue is a push segue - which it should be if you're using a UINavigationController - then the destination view controller is automatically added to the window hierarchy, and you don't even need to identify the UINavigationController:
if ([segue.identifier isEqualToString:#"yourSegueNameHere"]) {
[segue.destinationViewController setTitle:#"yourTitleHere"];
}

Resources