First steps with learning scrolling programming

Discussion in 'iOS Programming' started by StevenHu, Oct 9, 2009.

  1. StevenHu macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #1
    I'm just learning how to scroll with UIScroll. Most posts I've read about it involve people who already have scrolling implemented, and just need tweaking to get over a hump. I'm starting from scratch. It's not clear to me from the Apple docs where I put the scrolling code - on the same .m viewcontroller file that is to scroll? I am not using IB, for I am starting from Apple's UICatalog, ("SegmentViewController"), which is hard-coded. This is the code I have:

    Code:
    -(void)loadView {
    CGRect appFrame =[[UIScreen mainScreen] applicationFrame];
    myView = [[UIView alloc] initWithFrame:appFrame];
    self.view = myView;
    [myView release];
    
    myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    myScrollView setContentSize:CGSizeMake(320, 1000)];
    
    myScrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    myScrollView.contentSize = myView.frame.size;
    myScrollView.clipsToBounds = YES;
    myScrollView.scrollEnabled = YES;
    myScrollView.bounces = YES;
    myScrollView.scrollsToTop = YES;
    myScrollView.pagingEnabled = YES;
    myScrollView.delaysContentTouches = NO;
    myScrollView.showsVerticalScrollIndicator = YES;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.delegate = self;
    
    [myScrollView addSubView:myView];
    [myScrollView release];
    }
    First up is the AppDelegate, then the MainViewController (a TableView), then a UIView (SegmentViewController, which appears after tapping a cell in the MainViewController).

    It seems to me that the myView should be initWithName, and put the SegmentViewController there, but isn't the code supposed to work with ALL cells on the MainViewController? Or do we add the scroll code to each UIView we want scrolled? I know TableViews already implement scrolling by default.

    I'm not sure where to put it or how to link it to the views to be scrolled. Can you give me some pointers based on the info above?

    Thanks!
    Steve
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    My recommendation is to do this with IB. It should be working in ten minutes.

    The idea behind the scrollview is that you put your views inside the scrollview. You make the scrollview the view controller's view or some other view can host the scrollview. Then put your view or views to be scrolled inside the scrollview.

    In the code you show you don't add the scrollview to the existing view hierarchy.

    Look at the PageControl, Scrolling and ScrollViewSuite sample code.

    Here's a tip. To find sample code that uses a particular class, like UIScrollView, open up the documentation window. Type the name of the class into the search box. Choose a doc set like Apple iPhone OS 3.1. Choose Full-Text search. The results will include all the sample code that uses that class in its code.
     
  3. StevenHu thread starter macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #3
    So in IB, once I get a view and scroller working together, I'll be able to scroll the view in IB to continue adding controls and sliders that are below the view? I've never seen that in any tutorial.

    Thanks!
    Steve
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    In order to do that you need this kind of view hierarchy:

    container view
    .....scroll view
    ..........view to be scrolled
    .....other view, like button or toolbar
     
  5. StevenHu thread starter macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #5
    I want the user to click on Edit, then save his new settings (the settings are race car settings, like caster, camber, etc.). Then in the main view, the saved setting will appear as a new entry in the table. I thought that the copy feature required a single view. I don't want the user to click on every view separately to save his settings.

    It's entirely possible I am misunderstanding how the copy function will work. I haven't studied that part yet.

    Thanks,
    Steve
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  7. StevenHu thread starter macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #7
    When you add a contact (in the Contacts app on the iPhone), you first click on the plus sign at upper right. Then the contact appears on the table list. I am assuming that this feature is possible for views as well. That means that the view will appear when you click on the view you just made a copy of, but the view will show the last-saved control settings. This is the way I understand it.

    Thanks!
    Steve
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    I'm still not sure I understand what you're getting at. What is this view that "you just made a copy of"?

    Here's what's going on within the Contacts app: the main contact list is a tableview with a data-source supplying the information (probably an array of contact objects or something along those lines). When you click the + button to add a new contact, a modal view is presented that allows you to enter the specifics of the new contact. When you click the Done button, these specifics are saved into that data-source (via that contact object) and the modal view is dismissed. The table view that is the main list is reloaded and the new contact appears.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    The contacts app is a classic master-detail interface. The master list is the contacts list. When you tap an existing contact you go to a detail view that shows all the details of that contact and allows the user to edit those details. You can also add and remove items from the master list.

    UITableView and its related classes are designed and implemented so as to make it straightforward to implement master-detail interfaces.

    Steven, if you want to implement a master-detail interface using a tableview is almost certainly the best way to do it. If you use a scroll view and add subviews to it you will likely be duplicating UITableView functionality that already exists.

    At any rate, look at the settings app for a lot of use of table views for this kind of thing. Thousands of apps on the app store do this as well.
     
  10. StevenHu thread starter macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #10
    Thanks a lot for the replies. It's becoming clearer what direction I should be going in.

    I'm using a view because I can't drag the segmented buttons or sliders into a table cell in IB.

    The real problem is that this view will have just over 50 controls to be complete and I think it'll just crash.

    Regards,
    Steve
     

Share This Page