由于viewControllerBeforeViewController,UIPageViewController NavigationItem问题

时间:2015-02-09 10:15:48

标签: ios uinavigationcontroller uinavigationbar uipageviewcontroller viewpage

所以我有一个视图寻呼机,假设允许用户以用户期望在照片专辑中滑动的方式浏览照片,但问题出现在用户滑动并试图按下&#时34;份额"导航栏中的按钮。如果用户只是从相册中打开照片并且从不试图向右或向左滑动,它可以正常工作,但如果他们确实尝试向任一方向滑动然后尝试保存他们已滑动到的照片,则会在照片后返回照片用户当前正在查看。我把它消除了与viewControllerBeforeViewController和viewControllerAfterViewController有关的东西被调用后当前viewcontroller的callehere是我的类:

#import "APPChildViewController.h"
#import "CommentsViewController.h"

#import "ViewPhotosPagerVC.h"

@interface ViewPhotosPagerVC ()

@end

@implementation ViewPhotosPagerVC

- (void)viewDidLoad {
    [super viewDidLoad];

   // [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];


    CommentsViewController *initialViewController = [self viewControllerAtIndex:0];
    NSDictionary *dict =[_photoArray objectAtIndex:_startingIndex];
    NSLog(@"DICT:%@",dict);
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    NSLog(@"MSG_ID:%@",msg_id);
    initialViewController.msg_id=msg_id;
    initialViewController.navItem=self.navigationItem;
    initialViewController.delegate=self;

    initialViewController.theIndex=_startingIndex;
    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

- (CommentsViewController *)viewControllerAtIndex:(NSUInteger)index {


    NSLog(@"CommentVC_INDEX:%zd",index);
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CommentsViewController *childViewController=[storyboard instantiateViewControllerWithIdentifier:@"pagerPhotoComments"];


    if(_photoArray<=index){

        index=0;
    }

    NSDictionary *dict =[_photoArray objectAtIndex:index];
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    childViewController.msg_id=msg_id;
    childViewController.navItem=self.navigationItem;
    childViewController.delegate=self;
    childViewController.theIndex = index;


    return childViewController;

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSLog(@"BEFORE_PAGEVIEWCONTROL_CALLED");
    NSUInteger index = [(CommentsViewController *)viewController theIndex];

    if (index == 0) {
        NSInteger newIndex=_photoArray.count;
        newIndex--;
        return [self viewControllerAtIndex:newIndex];
    }


    // Decrease the index by 1 to return
    index--;



    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CommentsViewController *childViewController=[storyboard instantiateViewControllerWithIdentifier:@"pagerPhotoComments"];


    if(_photoArray<=index){

        index=0;
    }

    NSDictionary *dict =[_photoArray objectAtIndex:index];
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    childViewController.msg_id=msg_id;
    childViewController.delegate=self;
    childViewController.theIndex = index;

    return childViewController;

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSLog(@"AFTER_PAGEVIEWCONTROL_CALLED");

    NSUInteger index = [(CommentsViewController *)viewController theIndex];

    index++;

    if(_photoArray.count<=index){


        index=0;
    }

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CommentsViewController *childViewController=[storyboard instantiateViewControllerWithIdentifier:@"pagerPhotoComments"];


    if(_photoArray<=index){

        index=0;
    }

    NSDictionary *dict =[_photoArray objectAtIndex:index];
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    childViewController.msg_id=msg_id;
    childViewController.delegate=self;
    childViewController.theIndex = index;


    return childViewController;

}



- (void)viewWillDisappear:(BOOL)animated {


    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

- (void)sharePhoto:(UIImage *)shareImage andText:(NSString *)shareText withDict:(NSDictionary *)dict{
    //NSLog(@"SHARE_IMG_UPDATE_DICT:%@",dict);

    NSString *ALBUM_ID= [dict objectForKey:@"ALBUM_ID"];
    NSString *combinedShareText;
    if(![ALBUM_ID isEqualToString:@"0"]){

        NSArray *album_array=[dict objectForKey:@"album_info"];
        NSDictionary *album_dict=[album_array objectAtIndex:0];

        //NSLog(@"ALBUM_DICT:%@",album_dict);
        NSString *album_title=[album_dict objectForKey:@"title"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *first_name= [defaults objectForKey:@"first_name"];
        NSString *album_photo_count=[dict objectForKey:@"album_img_count"];


        combinedShareText= [NSString stringWithFormat:@"%@ shared a photo w. you from the event album:%@(%@ photos) ~view the rest of this album on  for IOS",first_name, album_title,album_photo_count];

        NSLog(@"SHARE_TEXT:%@",combinedShareText);

    }else{


        NSString *first_name=[dict objectForKey:@"first_name"];
        NSString *last_name=[dict objectForKey:@"last_name"];
        NSString *message =[dict objectForKey:@"message"];
        NSString *created_string=[dict objectForKey:@"created"];
        double created_double = created_string.doubleValue;
        NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double];
        NSString *ago = [date timeAgo];
        combinedShareText= [NSString stringWithFormat:@"'%@'~ posted by %@ %@ %@ | You'll thank me later: www.buhz.com",message,first_name,last_name,ago];

        NSLog(@"SHARE_TEXT:%@",combinedShareText);
    }

    if(![ALBUM_ID isEqualToString:@"0"]){

    UIImage *backgroundImage = shareImage;
    UIImage *watermarkImage = [UIImage imageNamed:@"ios_watermark_logo.png"];

    UIGraphicsBeginImageContext(backgroundImage.size);
    [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
    [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - 240, backgroundImage.size.height - 110, 230, 100)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();








        UIActivityViewController *controller =
        [[UIActivityViewController alloc]
         initWithActivityItems:@[combinedShareText,result]
         applicationActivities:nil];
         [self presentViewController:controller animated:YES completion:nil];
    }else{

        UIActivityViewController *controller =
        [[UIActivityViewController alloc]
         initWithActivityItems:@[combinedShareText,shareImage]
         applicationActivities:nil];
         [self presentViewController:controller animated:YES completion:nil];

    }









}







-(UIImage*) drawText:(NSString*) text
             inImage:(UIImage*)  image
             atPoint:(CGPoint)   point
{

    NSMutableAttributedString *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",text]];

    // text color
    [textStyle addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, textStyle.length)];

    // text font
    [textStyle addAttribute:NSFontAttributeName  value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, textStyle.length)];

    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, point.y, image.size.width-(point.x*2), image.size.height);
    [[UIColor whiteColor] set];

    // add text onto the image
    [textStyle drawInRect:CGRectIntegral(rect)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
- (void)shareUpdate:(NSString *)shareText withDict:(NSDictionary *)dict{

    NSLog(@"SHARE_TEXT_UPDATE_DICT:%@",dict);
    NSString *first_name=[dict objectForKey:@"first_name"];
    NSString *last_name=[dict objectForKey:@"last_name"];
    NSString *message =[dict objectForKey:@"message"];
    NSString *created_string=[dict objectForKey:@"created"];
    double created_double = created_string.doubleValue;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double];
    NSString *ago = [date timeAgo];
    NSString *combinedShareText= [NSString stringWithFormat:@"'%@'~ posted by %@ %@ %@ | Join me on www..com",message,first_name,last_name,ago];

    NSLog(@"SHARE_TEXT:%@",combinedShareText);

    UIActivityViewController *controller =
    [[UIActivityViewController alloc]
     initWithActivityItems:@[combinedShareText]
     applicationActivities:nil];

    [self presentViewController:controller animated:YES completion:nil];


}

@end

0 个答案:

没有答案