2013-08-16 7 views
5

पर पेजिंग के साथ UIScrollView जोड़ें मैं अपने मौजूदा व्यू कंट्रोलर से अलग-अलग दृश्यों के माध्यम से एक यूआईएसक्रॉल व्यू को जोड़ना चाहता हूं जो मेरे ऐप का मूल दृश्य है। मेरे साथ टैब बार और नेविगेशन बार नियंत्रक भी हैं। क्या मैं इस दृश्य नियंत्रक को एक स्क्रॉल व्यू जोड़ सकता हूं जो मैं चाहता हूं, और यदि ऐसा है, तो क्या कोई मुझे सही दिशा में इंगित कर सकता है कि इसके बारे में कैसे जाना है?मौजूदा UIViewController

यहां मेरा व्यू कंट्रोलर है।

#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 

के रूप में अपने UIScrollView के लिए एक संपत्ति घोषित करें, और सेट अपने ViewController ** ध्यान दें कि मैं कोड के साथ अपना 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

यह इस पंक्ति पर दुर्घटनाग्रस्त है: [सबव्यूज addSubview: [viewArray objectAtIndex: i]]; अपरिचित चयनकर्ता के साथ उदाहरण 0xc18acd0 – raginggoat

+0

किस पंक्ति में भेजा गया है? @ user2029585 – hgwhittle

+0

मैंने अपनी टिप्पणी संपादित की। मैं पहले दुर्घटना से प्रवेश मारा। – raginggoat

1

मैं शायद, UIPageViewController का इस्तेमाल करके उनकी एक नियमित रूप से पेजिंग scrollview से कोशिश करेंगे: कि ऐसा करने के लिए, इस UIScrollViewDelegate विधि ओवरराइड।

ऐप्पल का PhotoScroller sample codeUIPageViewController का उपयोग करने का एक बहुत अच्छा उदाहरण प्रदान करता है।

मेरे पास कुछ code on github भी है जो UIPageViewController को UIViewController सबक्लास के अंदर लोड करने के लिए फोटोस्क्रॉलर को संशोधित करता है। (एप्पल के नमूना कोड में, UIPageViewController रूट दृश्य नियंत्रक है।)

मैं प्रत्येक पृष्ठ में अलग UIViewController उपवर्गों के साथ एक UIPageViewController इस्तेमाल नहीं किया है (PhotoScroller नमूना कोड सभी पृष्ठों में PhotoViewController रों का उपयोग करता है), लेकिन मैं नहीं कर सकता देखें कि यह कोड में कुछ संशोधनों के साथ क्यों नहीं किया जा सका।