SegmentedControl Keep spot on website

Discussion in 'iOS Programming' started by newtoiphonesdk, Sep 16, 2010.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I have my app set up with a SegmentedControl to go between different webviews. Here is my code I have
    Code:
    - (IBAction)changesegment {
    	if (segment.selectedSegmentIndex == 0) {
    		[blogs loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.316apps.com/blogbinds.html"]]];
    	}
    	if (segment.selectedSegmentIndex == 1) {
    		[blogs loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.316apps.com/scoop.html"]]];
    	}
    	
    }
    With it set up like this, you cannot back and forth between the web views without losing your spot on one. Is there anyway I can alter this so that the first time the app is loaded and these are selected it will go to the webviews, but afterwards it will hold your place on each website?
     
  2. Don Kosak macrumors 6502a

    Don Kosak

    Joined:
    Mar 12, 2010
    Location:
    Hilo, Hawaii
    #2
    Two approaches to consider.

    If you're looking mostly to remember the scroll position of each webview, you can do that via a JavaScript call. That's the simple and memory efficient way of remembering state. It's not fast though.

    Another way of doing it would be to use two different uiwebviews. Position them directly on top of one another. Use your segmented control to decide which one to hide:

    blog1.hidden = YES;
    blog2.hidden = NO;

    That will keep the exact position and state of the UIWebView, at the expense of keeping both around.
     
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Thanks. The issue I find with this is that I can only set Back, Forward, Stop, and Reload buttons to one of the WebViews.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Save the scroll position of the current page before you load the new page. When you come back restore the scroll position. You should only restore the scroll position once the webpage has completed loading (there is a delegate method for that).
     
  5. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #5
    I don't mean the scroll position I mean the website that view is on. If the user selects a blog article to read, then wants to view the other blog listed I want the webview where they were looking at the first blog to not go back to the original webpage once they click back on the button.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Same answer different details: take a copy of the current URL and save it, restore it when you come back.
     
  7. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #7
    The first answer I was given with using 2 webviews and hiding one and displaying the other when segment is selected works well, but the Back Forward Stop and Reload actions can only be used with one of the webviews in the view controller.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Why? Just retarget them to the other webview when you switch which ones active.
     
  9. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010

Share This Page