Objective-C button problem

Calls dealloc too early

Page 1 of 1

2 Replies - 4958 Views - Last Post: 13 May 2010 - 05:45 PM

#1 adgarci  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 96
  • Joined: 26-August 09

Objective-C button problem

Posted 04 April 2010 - 07:45 PM

Lately I've been developing for the iPhone and iTouch, I used BetaWar's starter tutorials, then moved on to play with the tab controls, after finishing that, I went ahead and Googled hard coding the tabs (I used the tutorial here), it did not look to challenging. As my last errors were resolved, something happened...

Currently, I have two tabs, one with a button and text field, the second with a label, when a user enters something into the text field and presses the button, it should update the label, no the case. It ends the app, with no errors, exceptions or anything.

Starting with the three headers,

From: View1Controller.h
#import <UIKit/UIKit.h>
#import "HardCodeTabsAppDelegate.h" 

@class HardCodeTabsAppDelegate;

@interface View1Controller : UIViewController {
	UITextField *textField;
	UIButton *cmdButton;
	UIView *myView;
	HardCodeTabsAppDelegate *delegateRef; //communication between windows
}

@property(nonatomic, assign) UITextField *textField;
@property(nonatomic, assign) UIButton *cmdButton;
@property(nonatomic, assign) UIView *myView;
@property(nonatomic, assign) HardCodeTabsAppDelegate *delegateRef;

@end



From: View2Controller.h
#import <UIKit/UIKit.h>


@interface View2Controller : UIViewController {
	UILabel *label;
	UIView *myView;
}

@property(nonatomic, assign) UILabel *label;
@property(nonatomic, assign) UIView *myView;

@end



From: HardCodeTabsAppDeligate.h
#import <UIKit/UIKit.h>
#import "View1Controller.h"
#import "View2Controller.h"

@class TabBarViewController;
@class View1Controller;
@class View2Controller;

@interface HardCodeTabsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	UITabBarController *tabBarController;
	View1Controller *view1Controller;
	View2Controller *view2Controller;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, assign) UITabBarController *tabBarController;
@property (nonatomic, assign) View1Controller *view1Controller;
@property (nonatomic, assign) View2Controller *view2Controller;

@end



From: View1Controller.m
#import "View1Controller.h"


@implementation View1Controller
@synthesize myView;
@synthesize cmdButton;
@synthesize textField;
@synthesize delegateRef;

- (id)init //Should this have been in a different method? Or is okay as its own?
{
	if (self = [super init])
	{
		//initialaization codez
		self.title = @"Text and Button"; //tab bar title
		CGRect cgRct = CGRectMake(0, 0, 320, 480);//size and position
		myView = [[UIView alloc] initWithFrame:cgRct];
		myView.autoresizesSubviews = YES;
		self.view = myView;
		
		cmdButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
		cmdButton.frame = CGRectMake(100, 100, 100, 50);
		[cmdButton setTitle:@"Click Me Now!!" forState:UIControlStateNormal];
		cmdButton.backgroundColor = [UIColor clearColor];
		[cmdButton addTarget:self action:@selector(ButtonPress) forControlEvents:UIControlEventTouchUpInside];
		cmdButton.adjustsImageWhenHighlighted = YES;
		
		cgRct = CGRectMake(60, 170, 200, 50);
		textField = [[UITextField alloc] initWithFrame:cgRct];
		textField.text = @"Enter text here";
		textField.borderStyle = UITextBorderStyleBezel;
		
		[self.view addSubview:cmdButton];
		[self.view addSubview:textField];
	}
	return self;
}

-(void)ButtonPress:(id)sender //I think that the problem lies here somewhere
{
	[self.textField resignFirstResponder];
	self.delegateRef.view2Controller.label.text = self.textField.text;
}



From: View2Controller.m
@implementation View2Controller
@synthesize label;
@synthesize myView;

- (id)init
{
	if (self = [super init]) //Same as before, its own? Or is this okay?
	{
		self.title = @"The Label";
		CGRect cgRct = CGRectMake(0, 0, 320, 480);
		myView = [[UIView alloc] initWithFrame:cgRct];
		myView.autoresizesSubviews = YES;
		self.view = myView;
		
		cgRct = CGRectMake(60, 170, 200, 50);
		label = [[UILabel alloc] initWithFrame:cgRct];
		label.text = @"Test";
		
		[self.view addSubview:label];
	}
	return self;
}



From: HardCodeTabsAppDeligate.m
@implementation HardCodeTabsAppDelegate

@synthesize window;
@synthesize view1Controller;
@synthesize view2Controller;
@synthesize tabBarController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    tabBarController = [[UITabBarController alloc] init];
	view1Controller = [[View1Controller alloc] init];
	view2Controller = [[View2Controller alloc] init];
	
	view1Controller.delegateRef = self;
	
	tabBarController.viewControllers = [NSArray arrayWithObjects:view1Controller, view2Controller, nil];
	
	[window addSubview:tabBarController.view];
    [window makeKeyAndVisible];
}



I've looked through all of the code many times, but no problems show up, thank you for your help.

This post has been edited by adgarci: 05 April 2010 - 07:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Objective-C button problem

#2 garfinkle  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 87
  • Joined: 08-September 09

Re: Objective-C button problem

Posted 13 April 2010 - 01:24 PM

Hi Adgarci,

Just looking at your code on here the issue I spot is with the following line in you View1Controller.m file

[cmdButton addTarget:self action:@selector(ButtonPress) forControlEvents:UIControlEventTouchUpInside];



Your ButtonPress method is expecting a (id)sender parameter as a variable but you are not sending one (resulting in an invalid method call). You need to make sure when you use the action:@selector() parameter that you send the (id)sender with it...

To do this simply add a : after the method name in the selector so you will end up with the following

[cmdButton addTarget:self action:@selector(ButtonPress:) forControlEvents:UIControlEventTouchUpInside];



You will get errors returned if you click the enable breakpoints button next to the build and run on the tool bar...without breakpoints the app will just close, enable it and your app will pause and errors will be returned.

Hope it helps, if it doesn't solve the problem, give me a shout

This post has been edited by garfinkle: 13 April 2010 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 adgarci  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 96
  • Joined: 26-August 09

Re: Objective-C button problem

Posted 13 May 2010 - 05:45 PM

I just dies a little inside, I forgot to add break points, I'm too used to Visual Studio automatically debugging.


Thank You
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1