Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Why can't I send data to a textField on a different view.

Hi all, I'm new here so not 100% sure this is the correct place for this but I'm starting to pull my hair out on this one. I've tried and tried but no luck.

I'm trying to segue from a view where I have a date picker then back to the view before that view, when I'm segueing back to the previous view I want to send that data from the datepicker directly to a textfield named dateField.

Below is the code for the segue I've made:


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:id
{
if ([[segue identifier] isEqualToString:@\"GoBackToEventP2\"]) {
NSLog(@\"Set destination VC to *subEventVC\");
NSDate *choice = [ datePick date];
NSLog(@\"Assign date from picker to *choice\");
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@\"dd/MM/yyy HH:mm\"];
NSLog(@\"Set the format of date and time\");
NSString *formatteddate = [format stringFromDate:choice];
NSLog(@\"Store the date format in *formatteddate\");
NSLog(@\"Preparing to Segue\");
SubmitEventsP2 *subEventVC = [segue destinationViewController];
[subEventVC.dateField setText:formatteddate];
NSLog(@\"Should send the date to the textField\");
// NSString *viewchange = @\"SubmitEventsP2\" ;
NSLog(@\"%@\",formatteddate);
}
}


I'm unsure why it's not sending the data over.

If you need anymore information or code let me know as I'm at a loss here and like the I said above I'm ready to start pulling my hair out. lol

I'll be very grateful to anyone who helps me with this.

BTW I'm quite new to xcode and Ios development, let alone any programming at all.

I am enjoying iphone development though... :)
Post edited by LeightonW87 on

Replies

  • Duncan CDuncan C Posts: 8,033Tutorial Authors, Registered Users
    LeightonW87;435888 said:
    Hi all, I'm new here so not 100% sure this is the correct place for this but I'm starting to pull my hair out on this one. I've tried and tried but no luck.

    I'm trying to segue from a view where I have a date picker then back to the view before that view, when I'm segueing back to the previous view I want to send that data from the datepicker directly to a textfield named dateField.

    Below is the code for the segue I've made:


    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:id
    {
    if ([[segue identifier] isEqualToString:@\"GoBackToEventP2\"]) {
    NSLog(@\"Set destination VC to *subEventVC\");
    NSDate *choice = [ datePick date];
    NSLog(@\"Assign date from picker to *choice\");
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@\"dd/MM/yyy HH:mm\"];
    NSLog(@\"Set the format of date and time\");
    NSString *formatteddate = [format stringFromDate:choice];
    NSLog(@\"Store the date format in *formatteddate\");
    NSLog(@\"Preparing to Segue\");
    SubmitEventsP2 *subEventVC = [segue destinationViewController];
    [subEventVC.dateField setText:formatteddate];
    NSLog(@\"Should send the date to the textField\");
    // NSString *viewchange = @\"SubmitEventsP2\" ;
    NSLog(@\"%@\",formatteddate);
    }
    }


    I'm unsure why it's not sending the data over.

    If you need anymore information or code let me know as I'm at a loss here and like the I said above I'm ready to start pulling my hair out. lol

    I'll be very grateful to anyone who helps me with this.

    BTW I'm quite new to xcode and Ios development, let alone any programming at all.

    I am enjoying iphone development though... :)

    You can't and you shouldn't. You should treat a view controller's views as private. View controllers should be free to change the way they work internally, and how they display their data, without other parts of your program caring. As long as a view controller's public interface stays the same, the rest of your program remains unchanged. This is called encapsulation, and it's a really important design strategy for object oriented programming.

    The reason your code isn't working is likely because when you first create a view controller, it's views don't exist until it gets displayed for the first time. Thus, when you try to refer to another view controller's view, it is nil, so setting it to some value fails.

    Even if you got around that problem, you'd have another problem. When you leave a view controller and display another view controller, the no-longer-active view controller's views can be unloaded right away, or at any time in the future if the system runs low on memory. If that happens, then when you go back to that view controller, the contents of it's views will revert to their initial states, and changes you made from outside the view controller would be lost.

    If you want to pass a piece of data to another view controller, you should add a property to that view controller. Let's say it's a string that you want to display in view controller 2. Add a property "userMessage" to view controller 2.

    In view controller 1, set the string property.

    vc2.userMessage = @\"You're ugly and your mother dresses you funny.\";


    Then, in view controller 2, in the viewWillAppear method, if you want to take that message and display it in a label, take the string value and install it in a your label.


    - (void)viewWillAppear:(BOOL)animated
    {
    userMessageLabel.text = userMessage;
    [super viewWillAppear: animated];
    }
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • LeightonW87LeightonW87 Posts: 2New Users
    Ok, I think I understand some of what you said Duncan but does the fact I'm using a modal segue to go in to the new view and go back.

    In a Stanford Uni lecture, the lecturer said that modal is a bad segue to use for this as he said it puts the user in a mode what ever that means.

    I've also notice that I think it maybe opening new duplicate views rather than returning to the previous view, I put data in to the field manually and it's reset when it modal's back to the old view.

    Do I need to use navigation controllers to go back to the old view.


    Also I know this sounds bad but where would I set the property you mentioned about.
  • Duncan CDuncan C Posts: 8,033Tutorial Authors, Registered Users
    LeightonW87;435932 said:
    Ok, I think I understand some of what you said Duncan but does the fact I'm using a modal segue to go in to the new view and go back.

    In a Stanford Uni lecture, the lecturer said that modal is a bad segue to use for this as he said it puts the user in a mode what ever that means.

    I've also notice that I think it maybe opening new duplicate views rather than returning to the previous view, I put data in to the field manually and it's reset when it modal's back to the old view.

    Do I need to use navigation controllers to go back to the old view.


    Also I know this sounds bad but where would I set the property you mentioned about.
    You'd set it in prepareForSegue if you're using segues.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.