【问题标题】:UIView (subclass) trying to present UIImagePickerControllerUIView(子类)试图呈现 UIImagePickerController
【发布时间】:2013-04-30 16:36:57
【问题描述】:

我有一个父视图,可让您查看UITableView 中的帖子。在其Navigation Bar 中,我有一个发布按钮,按下该按钮会显示UIView subclass 并将其显示在屏幕顶部。我在UIView 上有一张图片,当我点击它时,我想展示UIImagePickerController 以允许用户选择一张图片发布到服务。我该怎么做,因为我的子视图不是视图控制器,它不能呈现 UIImagePickerController。

下面是我的子视图代码。

    #import "PostView.h"

    @implementation PostView

    @synthesize attachedLabel;
    @synthesize postButton;
    @synthesize textView;
    @synthesize characterLimit;
    @synthesize attachImage;

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            originalFrame = frame;
            NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"PostView" owner:self options:nil];
            PostView *view = [xib objectAtIndex:0];
            [view setBackgroundColor:[UIColor whiteColor]];
            [view setAlpha:0.7f];
            attachedLabel = [[UILabel alloc] initWithFrame:CGRectMake(204, 212, 56, 21)];
            attachedLabel.textColor = [UIColor blackColor];
            [attachedLabel setText:@"Attached"];
            attachedLabel.backgroundColor = [UIColor clearColor];
            attachedLabel.font = [UIFont fontWithName:text_font_name size:12.0];
            characterLimit = [[UILabel alloc] initWithFrame:CGRectMake(246, 13, 50, 21)];
            [characterLimit setTextAlignment:NSTextAlignmentRight];
            characterLimit.textColor = [UIColor blackColor];
            characterLimit.backgroundColor = [UIColor clearColor];
            characterLimit.font = [UIFont fontWithName:text_font_name size:12.0];
            attachImage = [[UIImageView alloc] initWithFrame:CGRectMake(270, 208, 30, 30)];
            [attachImage setImage:[UIImage imageNamed:@"attachphoto30x30.png"]];
            [self.textView setDelegate:self];
            [self.textView setAlpha:0.7f];
            [self.textView setTextColor:[UIColor whiteColor]];
            [self.textView setBackgroundColor:[UIColor clearColor]];
            self.layer.cornerRadius = 10.0f;
            self.layer.masksToBounds = YES;
            [self addSubview:view];
            [self addSubview:characterLimit];
            [self addSubview:attachedLabel];
            [self addSubview:attachImage];
        }
        return self;
    }

    - (IBAction)openCamera:(id)sender
    {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.delegate = self;
        //[self presentViewController:controller animated:YES completion:nil];
        NSLog(@"%@", @"Image Tapped");
    }

    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    {
        /*[picker dismissViewControllerAnimated:YES completion:nil];
        UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
        UIImage *scale = [image scaleToSize:CGSizeMake(320.0f, 548.0f)];
        imageData = UIImageJPEGRepresentation(scale, 1);
        encodedImage = [self Base64Encode:imageData];
        [attachedLabel setHidden:NO];
         */
    }

    #pragma mark Custom alert methods

    - (IBAction)postAction:(id)sender
    {
       [self hide];
    }

    - (void)show
    {
        //prepare attachImage
        attachImage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapAttach = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(openCamera:)];
        tapAttach.numberOfTapsRequired = 1;
        [self.attachImage addGestureRecognizer:tapAttach];


        isShown = YES;
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
        [UIView beginAnimations:@"showAlert" context:nil];
        [self setBackgroundColor:[UIColor clearColor]];
        [UIView setAnimationDelegate:self];
        self.transform = CGAffineTransformMakeScale(1.1, 1.1);
        self.alpha = 1;
        [UIView commitAnimations];
    }

    - (void)hide
    {
        isShown = NO;
        [UIView beginAnimations:@"hideAlert" context:nil];
        [UIView setAnimationDelegate:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"hidePostView_Notification" object:nil];
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
        [UIView commitAnimations];
    }

    - (void)toggle
    {
        if (isShown)
        {
            [self hide];
        } else
        {
            [self show];
        }
    }

    #pragma mark Animation delegate

    - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
    {
        if ([animationID isEqualToString:@"showAlert"])
        {
            if (finished)
            {
                [UIView beginAnimations:nil context:nil];
                self.transform = CGAffineTransformMakeScale(1.0, 1.0);
                [UIView commitAnimations];
            }
        } else if ([animationID isEqualToString:@"hideAlert"])
        {
            if (finished)
            {
                self.transform = CGAffineTransformMakeScale(1.0, 1.0);
                self.frame = originalFrame;
            }
        }
    }

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
    {
        return YES;
    }


    - (BOOL)textView:(UITextView *)textViewer shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
    {
        if ([string isEqualToString:@"\n"])
        {
            [textViewer resignFirstResponder];
        }
        return [self isAcceptableTextLength:textViewer.text.length + string.length - range.length];
    } 

    -(IBAction)checkIfCorrectLength:(id)sender
    {
        if (![self isAcceptableTextLength:self.textView.text.length])
        {
            // do something to make text shorter
        }
    }

    - (BOOL)isAcceptableTextLength:(NSUInteger)length
    {
        return length <= 160;
    }


    - (void)textViewDidChange:(UITextView *)textViewer
    {
        NSString *characters = [[NSString stringWithFormat:@"%d", textViewer.text.length] stringByAppendingString:@"/160"];
        NSLog(@"%@", characters);
        [self updateDisplay:characters];
    }

    -(void) updateDisplay : (NSString *)str
    {
        [self.characterLimit performSelectorOnMainThread : @ selector(setText : ) withObject:str waitUntilDone:YES];
    }

    @end

【问题讨论】:

  • 展示 UIImagePickerController 试试这个并告诉我结果,[(YourParentViewController *)[self.superview nextResponder] presentViewController:controller animated:YES completion:nil];
  • 如何从 uiview 的子类中呈现另一个 viewController,如 FirstViewController,而我的 parentviewcontroller 是“ViewController”...

标签: ios objective-c uiview uiimage uiimagepickercontroller


【解决方案1】:

是的,您不能从 UIView 子类中呈现视图控制器。

要解决这个问题,你可以使用你的子视图的父视图的 viewcontroller 类。
在您的子视图中调用 [self.superview nextResponder] 将返回您超级视图的视图控制器。
使用它,您可以展示您的 UIImagePicker 视图控制器。

要使用 presentViewController 方法,您应该将 [self.superview nextResponder] 转换为您的 parentviewcontroller 的类类型。
还要确保在 subview.m 文件中导入 parentview controller.h

 [(YourParentViewController *)[self.superview nextResponder] presentViewController:controller animated:YES completion:nil];

【讨论】:

  • 确保将父视图导入到子视图 m 文件中,而不是 h 文件中,否则会出现编译错误。
  • 我不确定我是否应该再问一个问题,或者我是否可以补充一点。我还在玩这个,在他们选择图像后我设置了 imageData = UIImageJPEGRepresentation(image, 1); .然后,我使用 base64encoding 来获取该图像的 NSString 表示,以将其放入要发送到 RESTful 服务的 json 消息中 self.encodedImage = [[NSMutableString alloc] initWithString:[self Base64Encode:self.imageData]]; .....在这个函数中,一切都设置得很好,但是当我按下 post 按钮时,encodedImage 为空,几乎就像 arc 不允许它被保留一样。
  • 如何在此处保留这两个变量的属性。属性(强,保留) NSMutableString *encodedImage;属性(强,保留) NSData *imageData;
  • 能否请您在同一个子类中为dismissViewController快速编写这行代码
【解决方案2】:

您应该提供UIViewController 子类而不是UIView 子类。

我还要说UIViewController 应该负责处理其视图的数据和操作逻辑。查看一些文档:

视图控制器基础知识: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html

UIViewController 类参考: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html

【讨论】:

  • 如果我改成这样,我可以将该 UIViewController 子类的框架设置为 CGRectMake(5, 50, 310, 245) 因此它不会在下面覆盖。我想做一个弹出效果,仍然显示数据显示在它下面。
  • 你应该能够做到这一点,但你需要阅读如何继承 UIViewController 以及如何/何时绘制组成其内容的视图。我很确定您应该提交一个UIViewController 而不仅仅是一个简单的UIView
  • 我不确定这是否正确,因为您不能将视图控制器呈现为子视图,所以这意味着我必须呈现它并推送它,我不能只是将它弹出顶部.
  • @Jarod,要呈现 UIImagePickerController 试试这个并让我知道结果,[(YourParentViewController *)[self.superview nextResponder] presentViewController:controller animated:YES completion:nil];
  • @Jarod,没错。您不会将 ViewController 呈现为子视图,而是以模态方式呈现一个单独的控制器而不是现有控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多