Help with example code on Xcode 7?

#1

Hi there…
I’ve been struggling with Xcode 7 and used the help from general discussion and here viewtopic.php?f=485&t=10511 and modified a little bit:

[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 = CGRectMake(160, 240, 100, 150);
BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.window addSubview:firstView];

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

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

}
[/code]

However when I tried to run it, everything is fine except there’s no red rectangular here but the whole screen of simulator is red…I experimented with different number on this line:

CGRect firstFrame = CGRectMake(160, 240, 100, 150);
but still the whole screen is red…I don’t know what’s going wrong here. Beside. if anyone can explain a little bit what’s going on in these lines, I would really appreciate it:

UIViewController *viewController = [[UIViewController alloc] init]; viewController.view = firstView; self.window.rootViewController = viewController;
any advice is sincerely welcome

#2

Try the following:

[code]- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

CGRect topFrame = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame: topFrame];

// Add a centered hypnosis view
const CGFloat W = topFrame.size.width;
const CGFloat H = topFrame.size.height;
CGRect firstFrame = CGRectMake (topFrame.origin.x + 0.25 * W, topFrame.origin.y + 0.25 * H,  0.5 * W, 0.5 * H);
BNRHypnosisView * firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];

UIViewController * viewController = [[UIViewController alloc] init];
// Replace the default view...
viewController.view = firstView;

// Make it the root view controller...
self.window.rootViewController = viewController;

[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];

return YES;

}[/code]
Please note that this is the old way of doing things. Learn how to work with storyboards.

#3

[quote=“ibex10”]Try the following:

[code]- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

CGRect topFrame = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame: topFrame];

// Add a centered hypnosis view
const CGFloat W = topFrame.size.width;
const CGFloat H = topFrame.size.height;
CGRect firstFrame = CGRectMake (topFrame.origin.x + 0.25 * W, topFrame.origin.y + 0.25 * H,  0.5 * W, 0.5 * H);
BNRHypnosisView * firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];

UIViewController * viewController = [[UIViewController alloc] init];
// Replace the default view...
viewController.view = firstView;

// Make it the root view controller...
self.window.rootViewController = viewController;

[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];

return YES;

}[/code]
Please note that this is the old way of doing things. Learn how to work with storyboards.[/quote]

Hey ibex. Thank you for your reply. I tried your things but it still only shows the whole screen in red. Any other ideas on this?

#4

The following code works (on Xcode Version 7.2 (7C68) on OS X 10.11.2 (15C50))

//
//  AppDelegate.m
//
//  RedRectangle
//
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    CGRect topFrame = [[UIScreen mainScreen] bounds];
    self.window = [[UIWindow alloc] initWithFrame: topFrame];
    
    // Add a centered view
    const CGFloat W = topFrame.size.width;
    const CGFloat H = topFrame.size.height;
    CGRect firstFrame = CGRectMake (topFrame.origin.x + 0.25 * W, topFrame.origin.y + 0.25 * H,  0.5 * W, 0.5 * H);
    UIView * firstView = [[UIView alloc] initWithFrame:firstFrame];    // Replace this with your custom view
    firstView.backgroundColor = [UIColor redColor];
    
    UIViewController * viewController = [[UIViewController alloc] init];
    
    // Replace the default view...
    [viewController.view addSubview:firstView];
    
    // Make this the root view controller...
    self.window.rootViewController = viewController;
    
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    
    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 inactive 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

Differences from the previous one:

    UIView * firstView = [[UIView alloc] initWithFrame:firstFrame];   // Replace this with your custom view
    ...
   // Replace the default view...
    [viewController.view addSubview:firstView];
#5

[quote=“ibex10”]The following code works (on Xcode Version 7.2 (7C68) on OS X 10.11.2 (15C50))

//
//  AppDelegate.m
//
//  RedRectangle
//
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    CGRect topFrame = [[UIScreen mainScreen] bounds];
    self.window = [[UIWindow alloc] initWithFrame: topFrame];
    
    // Add a centered view
    const CGFloat W = topFrame.size.width;
    const CGFloat H = topFrame.size.height;
    CGRect firstFrame = CGRectMake (topFrame.origin.x + 0.25 * W, topFrame.origin.y + 0.25 * H,  0.5 * W, 0.5 * H);
    UIView * firstView = [[UIView alloc] initWithFrame:firstFrame];    // Replace this with your custom view
    firstView.backgroundColor = [UIColor redColor];
    
    UIViewController * viewController = [[UIViewController alloc] init];
    
    // Replace the default view...
    [viewController.view addSubview:firstView];
    
    // Make this the root view controller...
    self.window.rootViewController = viewController;
    
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    
    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 inactive 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

Differences from the previous one:

UIView * firstView = [[UIView alloc] initWithFrame:firstFrame]; // Replace this with your custom view ... // Replace the default view... [viewController.view addSubview:firstView]; [/quote]
Thank you. It worked.