Another Silver Challenge


#1

The second silver challenge in 6. Subclassing UIView states:
Make it so a user of HypnosisView can change the text it displays. (Hint: You’ll need to give HypnosisView a text property).
(note: it looks like only some versions of the bnr book have this challenge).

So I added a text property that someone with access to the code can easily use to alter the displayed text. However, I think the challenge intends for us to insert a UITextField on the view such that an app user can tap it, enter text, and see that text displayed in place of “You are getting sleepy”.

How can one add such a text field without a xib being present? I tried adding a UITextView as a subview to both the scrollView and the view in HypnosisAppDelegate.m, as demonstrated here stackoverflow.com/questions/2728 … mmatically . But this did not display any text field on the app.

Thanks,


#2

You can write an entire app without ever using a NIB (XIB) file.

Here is a simple app with a Button and a TextView:

//
//  AppDelegate.m
//  Nibless
//

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor underPageBackgroundColor];
    [self.window makeKeyAndVisible];
    
    CGRect rect = CGRectMake (300, 100, 100, 50);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = rect;
    [self.window addSubview:button];

    rect = CGRectMake (100, 200, 500, 500);
    UITextView *textView = [[UITextView alloc] initWithFrame:rect];
    [self.window addSubview];
    
    return YES;
}
...
@end

For more information, see View Programming documentation.