2012-05-17 2 views
0

Я работаю над приложением для рисования iPad. У меня есть класс UIView, который я импортировал в UIView Controller. но я получаю ошибки на [self.view drawPic:image]; как ошибка: тип приемника для сообщения экземпляра uiview не объявляет метод с селектором 'drawPic'.Импорт UIView в UIViewController

Пожалуйста найти мой полный код ниже:

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

@interface signPage : UIViewController<UIImagePickerControllerDelegate>{ 

    } 
@property (retain, nonatomic) IBOutlet UIImageView * myImg; 

@end 



#import "signPage.h" 
#import "DrawView.h" 

@implementation signPage 
@synthesize myImg; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 



- (IBAction)clear { 
    [self.view cancelDrawing]; 
} 

- (IBAction)saveDrawing { 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageWriteToSavedPhotosAlbum(finishedPic, self, @selector(exitProg:didFinishSavingWithError:contextInfo:), nil); 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { 

    [self dismissModalViewControllerAnimated:YES]; 

    [self.view drawPic:image]; 

} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

    [self dismissModalViewControllerAnimated:YES]; 


} 

-(void)exitProg:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your picture has been saved" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; 
    [alertView show]; 


} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 




@end 

@interface DrawView : UIView { 

    UIImage *myPic; 
    NSMutableArray *myDrawing; 
} 



@end 


#import "DrawView.h" 


@implementation DrawView 

-(void)drawPic:(UIImage *)thisPic { 

    myPic = thisPic; 

    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect { 

    float newHeight; 
    float newWidth; 

    if (!myDrawing) { 
     myDrawing = [[NSMutableArray alloc] initWithCapacity:0]; 
    } 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    if (myPic != NULL) { 
     float ratio = myPic.size.height/151; 
     if (myPic.size.width/302 > ratio) { 
      ratio = myPic.size.width/302; 
     } 

     newHeight = myPic.size.height/ratio; 
     newWidth = myPic.size.width/ratio; 

     [myPic drawInRect:CGRectMake(109,552,newWidth,newHeight)]; 
    } 
    if ([myDrawing count] > 0) { 
     CGContextSetLineWidth(ctx, 5); 

     for (int i = 0 ; i < [myDrawing count] ; i++) { 
      NSArray *thisArray = [myDrawing objectAtIndex:i]; 

      if ([thisArray count] > 2) { 
       float thisX = [[thisArray objectAtIndex:0] floatValue]; 
       float thisY = [[thisArray objectAtIndex:1] floatValue]; 

       CGContextBeginPath(ctx); 
       CGContextMoveToPoint(ctx, thisX, thisY); 

       for (int j = 2; j < [thisArray count] ; j+=2) { 
        thisX = [[thisArray objectAtIndex:j] floatValue]; 
        thisY = [[thisArray objectAtIndex:j+1] floatValue]; 

        CGContextAddLineToPoint(ctx, thisX,thisY); 
       } 
       CGContextStrokePath(ctx); 
      } 
     } 
    } 
} 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]]; 

    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 

    [self setNeedsDisplay]; 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 

    [self setNeedsDisplay]; 

} 

-(void)cancelDrawing { 

    [myDrawing removeAllObjects]; 
    [self setNeedsDisplay]; 

} 




@end 

Просьба предложить мне.

ответ

2

Вы определили drawPic: только для вашего DrawView класс, который вы, кажется, не используете нигде в y наш UIViewController. Если вы хотите, чтобы функция была доступна для каждого класса UIView, вы должны определить DrawPic как category.

+0

Можете ли вы предоставить мне какой-нибудь примерный код для этого, я новичок в iPad app dev – pradeepj

+0

Просто перейдите по ссылке в последнем слове. – Alexander

+0

Подробнее о категориях [здесь] (http://macdevelopertips.com/objective-c/objective-c-categories.html) – Alexander

0

изменить ур имя свойства с целью MView или что-то еще

@class DrawView; 
@interface signPage : UIViewController<UIImagePickerControllerDelegate>{ 

    } 
@property (retain, nonatomic) IBOutlet UIImageView * myImg; 

@property (retain, nonatomic)DrawView * mView; 

@end 

в implelemtation,

@synthesize mView = _mView; 

теперь называем

[self.mView drawRect]; 
+1

Нету .. до сих пор его не working..u сказал, чтобы изменить [self.view drawPic: изображение]; в [self.mView drawPic: изображение]; правильно? – pradeepj

+0

@ The Saad: Это должно быть в комментарии. Если вы не уверены или даете предложение. – Devang

+0

@pradeepj. создайте свойство Drawview с именем mView. вы пытаетесь вызвать viewcontroller. но ваш метод находится в режиме рисования – Saad

0

Добавить UIView (тип класса DrawView) в SignPage.xib IBOutlet связать его с drawView в файле .h

@interface signPage : UIViewController<UIImagePickerControllerDelegate>{ 

    IBOutlet UIView  *drawView; 
} 

@property (retain, nonatomic) IBOutlet UIImageView * myImg; 

@end 

Где вам нужно позвонить в signPage.m методов DrawView доступа, как это:

DrawView*dView = (DrawView*)drawView; 
[dView drawPic:image]; 

кСТАТИ: signPage Класс должен быть signPage (первая буква)

+0

Нет, не работает !!! – pradeepj

Смежные вопросы