2013-08-16 32 views
5

Tôi muốn thêm UIScrollView với phân trang để xem qua các chế độ xem khác nhau từ trình điều khiển chế độ xem hiện tại của tôi, đây là chế độ xem gốc của ứng dụng của tôi. Tôi cũng có thanh tab và bộ điều khiển thanh điều hướng cùng với nó. Tôi có thể thêm một cái nhìn cuộn để điều khiển xem này để thực hiện những gì tôi muốn, và nếu như vậy, ai đó có thể chỉ cho tôi đi đúng hướng về làm thế nào để đi về nó?Thêm UIScrollView với Phân trang vào UIViewController hiện tại

Đây là bộ điều khiển chế độ xem của tôi.

#import "KFBViewController.h" 
#import "ListViewController.h" 
#import "ActionAlertsViewController.h" 
#import "MarketUpdatesViewController.h" 
#import "AgStoriesViewController.h" 
#import "KFBNewsViewController.h" 
#import "MemberBenefits.h" 
#import "SocialNetworks.h" 
#import "WebViewController.h" 
#import "YouTubeView.h" 
#import "KFBFlickrViewController.h" 
#import "RSFM.h" 
#import "UAPush.h" 
#import "TUSafariActivity.h" 

@interface KFBViewController() 
{ 

} 

@end 

@implementation KFBViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     self.title = NSLocalizedString(@"Home", @"Home"); 
     self.tabBarItem.image = [UIImage imageNamed:@"home"]; 
     self.navigationController.delegate = self; 
    } 
    return self; 
} 

- (void) showMenu 
{ 
    NSURL *urlToShare = [NSURL URLWithString:@"https://itunes.apple.com/us/app/kentucky-farm-bureau/id580530986?mt=8"]; 
    NSArray *activityItems = @[urlToShare]; 
    // TUSafariActivity *activity = [[TUSafariActivity alloc] init]; 

    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil]; 
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll]; 

    [self presentViewController:activityVC animated:TRUE completion:nil]; 
} 

- (IBAction)gotoSecondView 
{ 
    YouTubeView *youTubeView = [[YouTubeView alloc] initWithNibName:@"YouTubeView" bundle:nil]; 
    youTubeView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentViewController:youTubeView animated:YES completion:nil]; 
} 
- (IBAction)gotoPublicAffairs 
{ 
    ListViewController *publicAffairs = [[ListViewController alloc]initWithStyle:UITableViewStylePlain]; 
    WebViewController *wvc = [[WebViewController alloc]init]; 
    [publicAffairs setWebViewController:wvc]; 
    publicAffairs.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:publicAffairs animated:YES]; 
} 

- (IBAction)gotoActionAlerts 
{ 
    ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain]; 
    WebViewController *wvc = [[WebViewController alloc]init]; 
    [actionAlerts setWebViewController:wvc]; 
    actionAlerts.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:actionAlerts animated:YES]; 
} 

- (IBAction)gotoMarketUpdates 
{ 
    MarketUpdatesViewController *marketUpdates = [[MarketUpdatesViewController alloc]initWithStyle:UITableViewStylePlain]; 
    WebViewController *wvc = [[WebViewController alloc]init]; 
    [marketUpdates setWebViewController:wvc]; 
    marketUpdates.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:marketUpdates animated:YES]; 
} 

- (IBAction)gotoAgStories 
{ 
    AgStoriesViewController *agStories = [[AgStoriesViewController alloc]initWithStyle:UITableViewStylePlain]; 
    WebViewController *wvc = [[WebViewController alloc]init]; 
    [agStories setWebViewController:wvc]; 
    agStories.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:agStories animated:YES]; 
} 

- (IBAction)gotoKFBNews 
{ 
    KFBNewsViewController *kfbNews = [[KFBNewsViewController alloc]initWithStyle:UITableViewStylePlain]; 
    WebViewController *wvc = [[WebViewController alloc]init]; 
    [kfbNews setWebViewController:wvc]; 
    kfbNews.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:kfbNews animated:YES]; 
} 

- (IBAction)gotoMemberBenefits 
{ 
    MemberBenefits *memberBenefits = [[MemberBenefits alloc] initWithNibName:nil bundle:nil]; 
    memberBenefits.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:memberBenefits animated:YES]; 
} 

-(IBAction)gotoPhotos:(id)sender 
{ 
    KFBFlickrViewController *photosView = [[KFBFlickrViewController alloc] initWithNibName:@"KFBFlickrViewController" bundle:nil]; 
    [self.navigationController pushViewController:photosView animated:YES]; 
} 

- (IBAction)gotoSocialNetworks 
{ 
    SocialNetworks *socialNetworks = [[SocialNetworks alloc] initWithNibName:nil bundle:nil]; 
    socialNetworks.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:socialNetworks animated:YES]; 
} 

- (IBAction)gotoFarmMarkets 
{ 
    RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil]; 
    rsfm.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:rsfm animated:YES]; 
} 

- (IBAction)settingsButtonPressed:(id)sender 
{ 
    [UAPush openApnsSettings:self animated:YES]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.title = @"Home"; 

    UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"\u2699" style:UIBarButtonItemStyleBordered target:self action:@selector(settingsButtonPressed:)]; 
    [settingsButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:24], UITextAttributeFont,nil] forState:UIControlStateNormal]; 
    self.navigationItem.leftBarButtonItem = settingsButton; 

    UIBarButtonItem *systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showMenu)]; 
    self.navigationItem.rightBarButtonItem = systemAction; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

@end 

Trả lời

10

Điều này thực sự hoạt động tốt đối với tôi:

Khai báo thuộc tính cho UIScrollView của bạn, và thiết lập ViewController bạn như là một UIScrollViewDelegate

@interface ViewController : UIViewController<UIScrollViewDelegate> 

@property (strong, nonatomic) UIScrollView *theScrollView; 

@end 

** Lưu ý rằng tôi đang thiết lập mã số UIScrollView của mình, nhưng có thể dễ dàng thực hiện bằng XIB.

Trong viewDidLoad: của ViewController bạn

self.theScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 300)]; 
    self.theScrollView.delegate = self; 
    self.theScrollView.pagingEnabled = YES; 
    self.theScrollView.showsHorizontalScrollIndicator = NO;  
    self.theScrollView.showsVerticalScrollIndicator = NO; 
    [self.view addSubview:self.theScrollView]; 

    NSArray *viewArray = [NSArray arrayWithObjects://your UIViews]; 

    for(int i=0; i<viewArray.count; i++) 
    { 
     CGRect frame; 
     frame.origin.x = self.theScrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = self.theScrollView.frame.size; 

     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     [subview addSubview:[viewArray objectAtIndex:i]]; 
     [self.theScrollView addSubview:subview]; 
    } 

    self.theScrollView.contentSize = CGSizeMake(self.theScrollView.frame.size.width * viewArray.count, self.theScrollView.frame.size.height); 

Hầu hết scrollViews phân trang cũng có một UIPageControl liên kết với nó. Để làm điều đó, ghi đè UIScrollViewDelegate phương pháp này:

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    CGFloat pageWidth = self.theScrollView.frame.size.width; 
    int page = floor((self.theScrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    self.thePageControl.currentPage = page; 
} 
+0

Sự cố trên dòng này: [subview addSubview: [viewArray objectAtIndex: i]]; với bộ chọn không được nhận dạng được gửi tới trường hợp 0xc18acd0 – raginggoat

+0

Dòng nào? @ user2029585 – hgwhittle

+0

Tôi đã chỉnh sửa nhận xét của mình. Tôi tình cờ nhập vào trước. – raginggoat

1

Tôi có lẽ sẽ cố gắng sử dụng UIPageViewController, chứ không phải là một scrollview phân trang thông thường.

Apple PhotoScroller sample code cung cấp ví dụ rất hay về cách sử dụng UIPageViewController.

Tôi cũng có một số code on github sửa đổi PhotoScroller để tải UIPageViewController bên trong lớp con UIViewController. (Trong mẫu mã của Apple, các UIPageViewController là bộ điều khiển xem gốc.)

Tôi chưa bao giờ sử dụng một UIPageViewController với UIViewController lớp con khác nhau trong mỗi trang (mẫu mã PhotoScroller sử dụng PhotoViewController s trong tất cả các trang), nhưng tôi không thể xem tại sao nó không thể được thực hiện với một vài sửa đổi đối với mã.

Các vấn đề liên quan