I have a main view. It calls up a subView (UIView) like so:
In the .m of the view controller:
#import "SettingsView.h" //this is my UIView class
...
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"Settings"
owner:
self options:nil];
SettingsView *settingsView = (SettingsView *)[nibViews objectAtIndex:
0];
[self.view addSubview:settingsView];
This seems to work. initWithCoder gets called, then drawRect. The
xib really
only contains a UIImageView with a background image in it. In the
SettingsView class I have code that sets up it's UI:
.h:
#import <UIKit/UIKit.h>
@interface SettingsView : UIView {
}
@end
.m:
#import "SettingsView.h"
#import "AnalogAppDelegate.h"
- (void) pickOne:(id)sender {
//This doesn't seem to want to work
AnalogAppDelegate *appDelegate = (AnalogAppDelegate *)
[[UIApplication
sharedApplication] delegate];
switch ( [((UISegmentedControl *)sender) selectedSegmentIndex]) {
case 0:
[appDelegate stopSleep:YES]; //crashes app
break;
case 1:
[appDelegate stopSleep:NO]; //crashes app
break;
}
}
- (void)layoutSubviews {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(120, 300, 80, 30);
[btn setTitle:@"OK" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[self addSubview:btn];
NSArray *itemArray = [NSArray arrayWithObjects: @"Enabled",
@"Disabled",
nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]
initWithItems:itemArray];
segmentedControl.frame = CGRectMake(40, 44, 240, 29);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 1;
[segmentedControl addTarget:self action:@selector(pickOne:)
forControlEvents:UIControlEventValueChanged];
[self addSubview:segmentedControl];
}
- (void) aMethod:(id)sender {
[self removeFromSuperview];
}
In the AnalogAppDelegate I have this simple method:
.h:
- (void) stopSleep:(BOOL)shouldStop;
.m:
- (void) stopSleep:(BOOL)shouldStop {
NSLog(@"stop sleep %@", shouldStop);
}