iPhone: Question in regards to a UIView accessing AppDelegate
iPhone: Question in regards to a UIView accessing AppDelegate
- Subject: iPhone: Question in regards to a UIView accessing AppDelegate
- From: "Eric E. Dolecki" <email@hidden>
- Date: Wed, 23 Dec 2009 14:45:45 -0500
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);
}
I am wondering why the app crashes when I try this. I don't see anything in
the debug console, it just dies.
--
http://ericd.net
Interactive design and development
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden