Search code examples
xcodeuitextviewuistoryboardsegue

How can I change a text views content depending on a button press within a different view controller.


I have come across a simple issue that has totally flummoxed me for some reason.

I have one view controller (which I will call the Info page) which has a text view contained within it. The text view holds a large amount of information in the following format;

Header A

Information about Header A

Header B

Information about Header B

(and so on).

Previous to this, there are viewcontrollers for each of the Headers; on each of which there is a button to segue to the Info Page.

My problem is pretty simple. When the user presses the segue button to transfer to the Info page, how do I code a solution where by the text view automatically scrolls to the specific Header information.

For example, say the user is in the view controller for Header D. He or she clicks the button to segue to the Info page. Upon arrival, how do I make the text view scroll automatically to show the information for Header D.

My apologies if I haven't been clear enough with my question. Obviously I have set up the text box, and the segues... just this final set is alluding me even after spending an age looking around for the answer! Even just directing me to some reading on the subject I may have missed would be great.

Thanks for your help.


Solution

  • This might not be the cleanest solution to this, but this should work:

    This goes in the first ViewController's buttons. Just iterate through the buttons setting the integer from 1 to 4 depending on which button you press.

    -(void)firstBtnTUI
    {
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"HEADER"];
    
        //...
    }
    

    This goes in ViewDidLoad (or similar) in the pushed view controller

    int headerNum = [[NSUserDefaults standardUserDefaults] integerForKey:@"HEADER"];
    CGFloat offset;
    
    if(headerNum == 1)
    {
        offset = firstHeader.frame.origin.y;
    }else if(headerNum == 2)
    {
        offset = secondHeader.frame.origin.y;
    }else if(headerNum == 3)
    {
        offset = thirdHeader.frame.origin.y;
    }else if(headerNum == 4)
    {
        offset = fourthHeader.frame.origin.y;
    }
    
    [scrollview setContentOffset:offset animated:NO];