【问题标题】:AppDelegate complains of incomplete implementation, and crashes with a debug view full of pointersAppDelegate 抱怨实现不完整,并在充满指针的调试视图中崩溃
【发布时间】:2012-11-11 06:31:15
【问题描述】:

我的代码有问题,在我开始添加myImageView代码之前它工作正常,但是appDelegate不完整的实现警告已经存在很长时间了。

我的 appDelegate.h

//
//  AppDelegate.h
//  drawern
//
//  Created by Marin Jelica on 2012-11-21.
//  Copyright (c) 2012 Marin Jelica. All rights reserved.
//


#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate/*, UIScrollViewDelegate*/>
/*{
 //   IBOutlet UIScrollView *myScrollView;
 //   UIImageView *myImageView;
}*/

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
/*
@property (strong, nonatomic) UIScrollView *myScrollView;

@property (strong, nonatomic) UIImageView *myImageView;
*/
-(void)setRed;
-(void)setGreen;
-(void)setBlue;
@end

我的 appDelegate.M

//
//  AppDelegate.m
//  drawern
//
//  Created by Marin Jelica on 2012-11-21.
//  Copyright (c) 2012 Marin Jelica. All rights reserved.
//

#import "AppDelegate.h"

#import "ViewController.h"


@implementation AppDelegate

//@synthesize myImageView;
//@synthesize myScrollView;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   /* UIImage *myImage = [UIImage imageNamed:@"de_dust2.png"];
    myImageView = [[UIImageView alloc] initWithImage:myImage];

    myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
    myScrollView.maximumZoomScale = 4.0;
    myScrollView.minimumZoomScale = 0.75;
    myScrollView.clipsToBounds = YES;
    myScrollView.delegate = self;
    // [myScrollView addSubview:myImageView];*/

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    [self.viewController setRed:1];
    [self.viewController setGreen:0];
    [self.viewController setBlue:0];
    [self.viewController setAlpha:1];
    [self.viewController checkRotation];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

我的 viewController.h

//
//  ViewController.h
//  drawern
//
//  Created by Marin Jelica on 2012-11-21.
//  Copyright (c) 2012 Marin Jelica. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    CGPoint lastPoint;
    CGPoint moveBackTo;
    CGPoint currentPoint;
    CGPoint location;
    NSDate *lastClick;
    BOOL mouseSwiped;
    UIImageView *drawImage;
    UIImageView *frontImage;
}
- (IBAction)blueButton:(id)sender;
- (IBAction)greenButton:(id)sender;
- (void)checkRotation;

@property int red;
@property int green;
@property int blue;
@property int alpha;
@property int height;
@property int width;

@end

我的 viewController.m

//
//  ViewController.m
//  drawern
//
//  Created by Marin Jelica on 2012-11-21.
//  Copyright (c) 2012 Marin Jelica. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize red;
@synthesize green;
@synthesize blue;
@synthesize alpha;
@synthesize height;
@synthesize width;


- (void)viewDidLoad
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    drawImage.image = [defaults objectForKey:@"drawImageKey"];
    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = self.view.frame;
    [self.view addSubview:drawImage];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    width=3000;
    height=3000;
    // UIImage *image = [[UIImage alloc] init]


   /*
    UIImage *myImage = [UIImage imageNamed:@"de_dust2.png"];
    myImageView = [[UIImageView alloc] initWithImage:myImage];

    myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
    myScrollView.maximumZoomScale = 4.0;
    myScrollView.minimumZoomScale = 0.75;
    myScrollView.clipsToBounds = YES;
    myScrollView.delegate = self;
    // [myScrollView addSubview:myImageView];*/
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
    }


    location = [touch locationInView:touch.view];
    lastClick = [NSDate date];

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 0;

    [super touchesBegan: touches withEvent: event];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
    currentPoint = [touch locationInView:self.view];

    UIGraphicsBeginImageContext(CGSizeMake(width , height));
    [drawImage.image drawInRect:CGRectMake(0, 0, width, height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, alpha);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());


    [drawImage setFrame:CGRectMake(0, 0, width, height)];
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastPoint = currentPoint;

    [self.view addSubview:drawImage];
}


- (IBAction)blueButton:(id)sender {
    blue=1;
    red=0;
    green=0;
}


- (IBAction)greenButton:(id)sender {
 green=1;
    blue=0;
    red=0;   
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self checkRotation];
}


-(void)checkRotation{

    NSLog(@"Width:%i", width);
    NSLog(@"Height:%i", height);


    UIInterfaceOrientation orientation = self.interfaceOrientation;
    if (orientation==UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
     //   width = 3000;
     //   height = 3000;


        [drawImage setFrame:CGRectMake(0, 0, width, height)];
     //   drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        lastPoint = currentPoint;

        [self.view addSubview:drawImage];
    }

    if (UIInterfaceOrientationIsPortrait(orientation)) {
     //   width = 3000;
     //   height = 3000;


        [drawImage setFrame:CGRectMake(0, 0, width, height)];
   //     drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        lastPoint = currentPoint;

        [self.view addSubview:drawImage];
    }
}

@end

这是启动时崩溃时发生的情况的图像

【问题讨论】:

  • 拥有崩溃的回溯和错误消息会很有帮助。仅仅知道它崩溃是不够的。

标签: objective-c ios cocoa-touch cocoa uiscrollview


【解决方案1】:

由于您在appdelegate.h 文件中添加了以下方法定义,因此预计它会在appdelegate.m 中实现,但编译器无法在那里找到它。这就是它显示该警告的原因。

-(void)setRed;
-(void)setGreen;
-(void)setBlue;

您可以删除这些行或在 .m 文件中实现相同的内容。

- (void)setRed {
  //set red color here
}

- (void)setGreen {
  //set green color here
}

- (void)setBlue {
  //set blue color here
}

你需要改变这一行,

drawImage.image = [defaults objectForKey:@"drawImageKey"];
drawImage = [[UIImageView alloc] initWithImage:nil];

把它当做,

UIImage *image = //get the image from defaults.. Is [defaults objectForKey:@"drawImageKey"] returning NSData of UIImage object? If not create UIImage object using the return name from this key
//if it returns a string, you can do the following
UIImage *image = [UIImage imageNamed:[defaults objectForKey:@"drawImageKey"]];

drawImage = [[UIImageView alloc] initWithImage:image];    

始终确保[super viewDidLoad]; 是方法中的第一个调用。

- (void)viewDidLoad 
  {
     [super viewDidLoad];

【讨论】:

  • 非常感谢您的帮助,我已经修复了您提到的大部分内容,还没有修复 U​​IImage 问题,但是当我构建时我没有收到任何警告没有错误,它只是崩溃了并向我展示了一个带有exc_breakpoint code exc_i386_bpt 错误的调试标记指针,知道原因,再次感谢您迄今为止的帮助!
  • @Xe3d,您确定此图像在默认情况下以某种形式存在吗?你也必须改变它,因为如果你在你的代码中这样做,它会崩溃。尝试更改为第二个选项。如果它是您从默认值中获取的字符串,它将在该行崩溃。请编辑问题并复制您的崩溃日志并将其粘贴到问题下方。据我所知,您无法将 UIImage 保存为该表单的默认值。
  • 你的意思是在日志导航器中,因为它说它构建没有问题,我也是一个菜鸟,所以你的默认值是什么意思?
  • @Xe3d,不,我说的是这一行,drawImage.image = [defaults objectForKey:@"drawImageKey"]; 你为什么这样做?这可能会编译,但会像您提到的那样崩溃。你想在这条线上做什么?
  • 不知道,正在按照教程,按照他的步骤,但是什么是默认的,以及如何从中获取图像,我想我首先必须给它一张图片
猜你喜欢
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 2018-07-23
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
相关资源
最近更新 更多