2013-04-30 53 views
3

我有一個父視圖,允許您在UITableView中查看帖子。在我的Navigation Bar中,我有一個帖子按鈕,當按下該按鈕時,會出現一個UIView subclass並將其顯示在屏幕的頂部。我在UIView上有一個圖像,當點擊時,我想呈現UIImagePickerController以允許用戶選擇要發佈到該服務的圖像。我怎麼能做到這一點,因爲我的子視圖不是視圖控制器,它不能呈現UIImagePickerController。試圖呈現UIImagePickerController的UIView(子類)

下面是我的子視圖代碼。

#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 
+0

呈現的UIImagePickerController試試這個,讓我知道結果,[(YourParentViewController *)[self.superview nextResponder] presentViewController:控制器動畫:是完成:無]; – 2013-04-30 16:45:41

+0

如何從uiview的子類和我的parentviewcontroller呈現另一個viewController像FirstViewController是「ViewController」... – 2014-10-30 12:32:24

回答

5

是的,你不能從一個UIView子類呈現視圖 - 控制。

要解決此問題,您可以使用您的子視圖的超級視圖的viewcontroller類。
在您的子視圖中調用[self.superview nextResponder]將返回您superview的viewcontroller。
使用它可以呈現您的UIImagePicker視圖控制器。

要使用presentViewController方法,您應該將[self.superview nextResponder]強制轉換爲您的parentviewcontroller的類類型。
還要確保您導入parentview或者Controller.h subview.m文件中

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

確保您在子視圖m文件中導入父視圖,而不是h文件,否則您將收到編譯錯誤。 – Jarod 2013-04-30 17:14:24

+0

是的,當然。謝謝 – 2013-04-30 17:15:40

+0

我不知道我是否應該問另外一個問題,或者如果我可以堆一點。我仍然在玩這個和他們選擇圖像後我設置imageData = UIImageJPEGRepresentation(圖像,1); 。然後,我使用base64encoding獲取該圖像的NSString表示,以將其放入要發送給restful服務的json消息中。 .....在這個函數中它都設置得很好,但是當我按下post按鈕時,encodedImage爲null,就像arc不允許它被保留一樣。 – Jarod 2013-04-30 18:26:40

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

+0

如果我改變,我可以將該UIViewController子類的框架設置爲CGRectMake(5,50,310,245)所以它不包括在下面。我想做一個彈出效果,並仍然顯示數據顯示在它下面。 – Jarod 2013-04-30 16:40:47

+0

你應該可以做到這一點,但是你需要閱讀如何子類'UIViewController'以及如何/何時組成它的內容的視圖被繪製。我很確定你應該提供一個'UIViewController'而不是一個簡單的'UIView'。 – Aaron 2013-04-30 16:43:39

+0

我不確定這是否正確,因爲您無法將視圖控制器顯示爲子視圖,因此這意味着我必須將其呈現並推送給它,我不能將它彈出頂部。 – Jarod 2013-04-30 16:48:02