3

我有一个父视图,可让您在UITableView. 在其中Navigation Bar,我有一个发布按钮,按下该按钮时会显示 aUIView 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
4

2 回答 2

5

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

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

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

 [(YourParentViewController *)[self.superview nextResponder] presentViewController:controller animated:YES completion:nil];
于 2013-04-30T17:02:37.897 回答
0

你应该提出一个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

于 2013-04-30T16:39:17.700 回答