2013-08-16 2 views
5

내 응용 프로그램의 루트보기 인 기존보기 컨트롤러와 다른보기를 수행하는 페이징과 함께 UIScrollView를 추가하려고합니다. 또한 탭 막대와 탐색 막대 컨트롤러가 함께 있습니다. 이 뷰 컨트롤러에 스크롤 뷰를 추가하여 원하는 것을 성취 할 수 있습니까? 그렇다면 누군가가 올바른 방향으로 이동하여 방향을 알려줄 수 있습니까?기존 UIViewController에 페이징을 사용하여 UIScrollView 추가

여기 내보기 컨트롤러입니다.

#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 

답변

1

이 링크는 귀하를 돕습니다. 다음 페이지 컨트롤러와있는 ScrollView 모두 사용 -이 나를 위해 정말 잘 작동

PageControl Example in iPhone

10

:

UIScrollViewDelegate

@interface ViewController : UIViewController<UIScrollViewDelegate> 

@property (strong, nonatomic) UIScrollView *theScrollView; 

@end 

로의 ViewController를 당신의 UIScrollView에 대한 속성을 선언하고 설정 ** 코드를 사용하여 UIScrollView을 설정하고 있지만 XIB를 사용하여 쉽게 수행 할 수 있습니다.

은 당신의 ViewController의 viewDidLoad:에서

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); 

대부분의 페이징 scrollViews 또한 그와 관련된 UIPageControl 있습니다.

- (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

이 줄에서 충돌이 발생합니다. [subview addSubview : [viewArray objectAtIndex : i]]; 인식 할 수없는 선택기를 인스턴스 0xc18acd0으로 보냄 – raginggoat

+0

어떤 행을 사용합니까? @ user2029585 – hgwhittle

+0

내 의견을 편집했습니다. 나는 우연히 전에 우연히 타격을 가했다. – raginggoat

1

나는 아마 오히려 일반 페이징있는 ScrollView보다 UIPageViewController를 사용하려고 할 것입니다 :이를 위해이 UIScrollViewDelegate 메소드를 오버라이드 (override).

Apple의 PhotoScroller sample codeUIPageViewController을 사용하는 좋은 예입니다.

code on github 또한 PhotoScroller를 수정하여 UIViewController 하위 클래스에 UIPageViewController을로드합니다. (애플의 샘플 코드에서 UIPageViewController 루트 뷰 컨트롤러입니다.)

내가 각 페이지에서 다른 UIViewController 서브 클래스와 UIPageViewController을 사용한 적이합니다 (PhotoScroller 샘플 코드는 모든 페이지에 PhotoViewController의 사용),하지만 난 할 수 없습니다 코드를 약간 수정하면 왜이 작업을 수행 할 수 없는지보십시오.