Xcode7 can‘t run the example code


#1

the 4 and 5 chapters example code can’t work as well

but the 6 chapter can run very well。。。。。。

i have no ideal and don’t know why… :open_mouth: :open_mouth: :open_mouth: :open_mouth:

the Xcode 7 return me “thread 1:signal SIGABRT”

and return this:
Hypnosister[15797:1176732] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Application windows are expected to have a root view controller at the end of application launch’
*** First throw call stack:
(
0 CoreFoundation 0x0000000104f08f65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000104982deb objc_exception_throw + 48
2 CoreFoundation 0x0000000104f08dca +[NSException raise:format:arguments:] + 106
3 Foundation 0x00000001045d0ae2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 UIKit 0x000000010309c275 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2875
5 UIKit 0x0000000103098ff0 -[UIApplication workspaceDidEndTransaction:] + 188
6 FrontBoardServices 0x000000010723a7ac -[FBSSerialQueue _performNext] + 192
7 FrontBoardServices 0x000000010723ab1a -[FBSSerialQueue _performNextFromRunLoopSource] + 45
8 CoreFoundation 0x0000000104e350a1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
9 CoreFoundation 0x0000000104e2afcc __CFRunLoopDoSources0 + 556
10 CoreFoundation 0x0000000104e2a483 __CFRunLoopRun + 867
11 CoreFoundation 0x0000000104e29e98 CFRunLoopRunSpecific + 488
12 UIKit 0x000000010309898d -[UIApplication _run] + 402
13 UIKit 0x000000010309d676 UIApplicationMain + 171
14 Hypnosister 0x0000000102dc546f main + 111
15 libdyld.dylib 0x000000010599392d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)


#2

Xcode 7 unfortunately does not have a empty template anymore when you create a new project. In order to get your starting environment similar to the one in the book see: viewtopic.php?f=467&t=10467&p=30175&hilit=Xcode+empty+template#p30175

After you have a project that does not rely on storyboards you can start implementing the book’s code. I found that in order to avoid the error you are seeing, you have to declare a root view controller. The book says this is not necessary but if you want to get your code running in Xcode 7 it is. You also have to set the view instance variable of your view controller to point to the new view instance you created. If you do not do this, I found that when the phone orientation changes, drawRect is not called.

[code]

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    CGRect firstFrame = self.window.bounds;
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    firstView.backgroundColor = [UIColor redColor];
    [self.window addSubview:firstView];

    ViewController *viewController = [[ViewController alloc] init];
    viewController.view = firstView;
    self.window.rootViewController = viewController;

    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    return YES;
    }[/code]


#3

Try this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    UIViewController *viewController = [[UIViewController alloc]init];
    [self.window setRootViewController:viewController];
    
    CGRect firstFrame = self.window.bounds;
    
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    
    [self.window addSubview:firstView];

    NSLog(@"width: %f; height: %f", self.window.frame.size.width, self.window.frame.size.height);
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}

#4

Thanks Saltysugar, your solution worked for me :slight_smile:


#5

In Xcode 7,there is no empty application. So I use single view application.Then you will see 4 files, like AppDelegate.h , AppDelegate.m, ViewController.h, ViewController.m. And as book goes, create BNRHypnosister.h .m, Then…

[code]self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect firstFrame = CGRectMake(160, 240, 100, 150);

BNRHypnosisterView *firstView = [[BNRHypnosisterView alloc] initWithFrame:firstFrame];

ViewController *viewController = [[ViewController alloc] init];
viewController.view = firstView;

self.window.rootViewController = viewController;

firstView.backgroundColor = [UIColor redColor];

[self.window addSubview:firstView];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];[/code]

I think the biggest problem is about root view controller and the empty application.


#6

I am using the following in didFinishLaunchingWithOptions

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController = [[UIViewController alloc]init];
[self.window setRootViewController:viewController];
CGRect firstframe=CGRectMake(240, 240, 700, 700);
HypnosisView *firstView=[[HypnosisView alloc]initWithFrame:firstframe];
firstView.backgroundColor=[UIColor redColor];
[self.window addSubview:firstView];
CGRect secondFrame = CGRectMake(20, 30, 500, 500);
HypnosisView *secondView = [[HypnosisView alloc] initWithFrame:secondFrame];
secondView.backgroundColor = [UIColor blueColor];
[self.window addSubview:secondView];

I see both red and blue frames on screen but are placed in the top left corner of screen not at specified positions. Can somebody please help?


#7
  1. Make it easier for others to view your code by posting it properly formatted;

  2. Add the subviews to the view controller’s view, not to the window.


#9

// delete this line [self.window addSubview:firstView];
// add this
ViewController *vc =[[ViewController alloc] init];
vc.view = firstView;
self.window.rootViewController = vc;

you need a ViewController and add view to ViewController