prepareForSegue UITableViewController using push - ios

I'm having some trouble passing data from my UITableView to a UIViewController. I'm using PUSH in storyboard to go from my Cell to the UIViewController, and everything is working well. When I select a cell in my UITableView it pushes to the UIViewController and I get a nice back button to return to the UITableView. My problem is passing data to the UIViewController using the PrepareForSegue method ... this is what I'm doing:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowUserDetailSegue"])
{
UserDetailViewController *userInfo = segue.destinationViewController;
userInfo.myLabel.text = #"Hello World";
NSLog(#"I'm here");
}
}
The label "myLabel" does not change when the UIViewController is displayed. Any idea why?
I've used prepareForSegue before and have been able to pass data, not sure what I'm missing.
Also, xcode is showing all the proper code completions and there are no errors.
Thanks!

At the prepareForSegue stage the destination viewController hasn't yet loaded it's view so userInfo.myLabel is nil.
Add a new property to UserDetailViewController, set that to your string instead and in viewDidLoad set it up in your label.

Related

Pass object to SASlideMenuViewController subclass

After login succeeds my LoginViewController will loginSegue to the SASlideMenuRootViewController, which then in its own viewDidLoad automatically looks for a leftMenu segue (that I have set up to my MenuViewController) and sets this up as its leftMenu property.
This all works great but I can't see how I am supposed to pass dependencies from the LoginViewController to my MenuViewController.
Have tried the following as is the usual way to do this but it fails as obviously the leftMenu property hasn't yet been configured as viewDidLoad doesn't run until after this.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"loginSegue"])
{
// This works
SASlideMenuRootViewController *slideViewController = (SASlideMenuRootViewController *)segue.destinationViewController;
// menuViewController is nil :(
MenuViewController *menuViewController = (MenuViewController *)slideViewController.leftMenu;
// I want to pass this along
menuViewController.account = self.account;
}
}
Do I need to subclass SASlideMenuRootViewController just to pass this value along? Seems messy.
https://github.com/stefanoa/SASlideMenu
I ended up having to subclass SASlideMenuRootViewController just to pass this along.

Pass data forward by Segue (objective-c)

I have 2 view controller
Main has a button
Detail has a label: lblDetail
I'm trying to pass data by segue : "sgPushDetail". When click on the button on Main, the label on Detail will be changed.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"sgPushDetail"]) {
DetailViewController *detail = segue.destinationViewController;
detail.lblDetail.text = #"you've already clicked";
}
}
But when i build, the label doesn't change. Please help me with this case!
The DetailViewController is not loaded when you are setting value for the label in prepareForSegue method. Do the following:
Create a property (i.e. NSString) in DetailViewController
Set the value for this property in prepareForSegue method
In viewDidLoad method of your DetailViewController, assign this property value to the label.

IOS - Use Segue identifier in child method

This is probably a basic question - but I cant seem to find the answer to it in my searches!
I have a uiview setup which contains two input areas which are linked to a child modal view via separate segues.
this is my parent prepare for segue method -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goalInfo"])
{
[segue.destinationViewController setYourGoalViewController:self];
}
if ([segue.identifier isEqualToString:#"longGoalInfo"])
{
[segue.destinationViewController setYourGoalViewController:self];
}
}
basically i'd like to determine which segue had been used in the child view so I can apply the relevant filed updates and alter a title/description in the child view. - I basically need something very similar to
if ([segue.identifier isEqualToString:#"goalInfo"])
but i'm not sure how to access this from teh child? Any tips?
The segue identifier is check on the source ViewController during its prepareForSegue: method. What you rather need is during prepare for segue is to set properties on the destination viewController and then when it loads, read these properties and set UI outlets.
Look at this example: I am stuck at presenting a full image on tapping the cell in UICollectionViewController. It's about CollectionVC and its detailVC, but the principle is the same.

Segue passing data but not animating the ViewController presentation

I have this problem:
I have two ViewControllers.
I am transitioning to the second view with Segue.
User enters his name on first view controller and taps on a button. If the text is nil, it should not show the second view controller. If some text is there on the text field - it has to show the next view controller.
I am checking the text length here below. As I have only one segue... I am not checking segue identifier.
And, on Storyboard, I have give modal transition CoverVertical. The animation is not working. View is just appearing. UIModalTransitionStyle also I tried. Still not working (on device and simulator)
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if(playerNameTextField.text.length == 0)
{
UIColor *tempColor=UIColorFromRGB(0xFF4981);
[self colorizeTextViewForAWhile:playerNameTextField withUIColor:tempColor animated:YES];
return NO;
}
return YES;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"segueMoveToHome"])
{
ViewController *vc = (ViewController *)segue.destinationViewController;
vc.playerName=playerNameTextField.text;
}
}
Your code works perfectly. I just reproduced and I've got a transition with CoverVertical.
In Interface Builder, select the Storyboard Segue and be sure that you have:
I'm linking you a mini demo with your code that works.
I have removed the Identifier text and continued. As it was the only segue or any other reason... it was working. If I put the segue... it is not working. :| Not sure. But I have now my app working.

Steps on how to push segue to tableviewcontroller

I am having issues displaying data in a tableview from a push segue. How do you make data appear in the tableview from the push segue? Do I need to implement a delegate/protocol? What method would I add my logic to display the rows in the TableViewController from the push segue. I able to navigate to the tableview and back to the button sort view controllers.
Can someone show me the process similar how I wrote My Logic? I am stuck on step 4. If you have a easier way to execute please let me know.
Controllers I am trying to coordinate.
"EventFeedController.m " TableViewController executed from Tab Bar Controller. This shows the events.
" ButtonSort.m " to push segue to EventFeedController.m
AppDelegate.m has array.
My Logic...
In the storyboard.
Control drag from UIButton in ButtonSort directly to EventsFeedViewController (not to the navigationcontroller but directly to the view EventViewController itself. ). (Not sure if I should control drag to view controller or the navigation controller of EventsfeedViewController)
Select push segue from popup of the control drag
Give Button a identifier of Button01 (This would find items array where eventFeed.eventType = #"Festival";) So for clarity Button01 = Find events with eventFeed.eventType = #"Festival";
Step I am stuck on. Not sure how to access or display the array created in AppDelegate.m didFinishLaunchingWithOptions paragraph. I thought cellForRowAtIndexPath in EventsFeedViewController would be responsible for being called and displaying the rows again.
Sample of array code AppDelegate.m
Events array created AppDelegate.m. I need to pull the event when clicking the push button segue.
_events = [NSMutableArray arrayWithCapacity:20];
//***************************************
// Event feed related area
//***************************************
SDEventsFeed *eventFeed = [[SDEventsFeed alloc] init];
eventFeed.eventTitle = #"Event Fest Test 1";
eventFeed.eventDescription = #"This is not to be missed";
eventFeed.eventStartTime = #"2:00 PM";
eventFeed.eventImage = [UIImage imageNamed:#"fest.jpg"];
eventFeed.eventType = #"Festival";
[_events addObject:eventFeed];
In the table view controller that you want to push to display the data, you will need to add a property to hold that data. Something like the following:
#property (nonatomic, strong) MyDataClass *myData;
Then in the view controller that you're pushing from (the one with the button that pushes the table view controller), you'll want to add a method - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender. You can implement it in a way like the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"YourSegueIdentifierFromStoryboard"]) {
YourTableViewControllerClass *viewController = segue.destinationViewController;
viewController.myData = self.myData;
}
}
To pull the data from the AppDelegate you would need to do the following.
Make sure you import the AppDelegate.h into your class doing the segue.
You can create a NSMutableArray property in AppDelegate.h to hold the array data "eventArray".
This is just one possible solution.
ButtonSort.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Button01"]) {
YourTableViewControllerClass *viewController = segue.destinationViewController;
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
viewController.events = appDelegate.eventsArray;
}
}

Resources