xcode viewDidLoad - xcode

I'm getting my feet wet with objective-C. And thought I'd play with iphone development.
I have a Page Controller.h and .m
Inside my implementation file I've thrown a UIButton inside the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
// content that loads in view
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 160, 300, 44);
[myButton setTitle:#"Press Me!" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont systemFontOfSize:14];
// add to a view
[self.view addSubview:myButton];
}
So far it's showing fine. This makes me ask about the difference between loadView vs viewDidLoad.
Also, I want to run my own function when the button is pressed, but I constantly get an executable error from xcode.
Here's what I've interjected within the code above:
- (void)viewDidLoad
{
[super viewDidLoad];
// content that loads in view
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 160, 300, 44);
[myButton setTitle:#"Press Me!" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont systemFontOfSize:14];
//added this
[myButton addTarget:self action:#selector(runClick) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:myButton];
}
-(void)runClick{
NSLog(#"does this work?");
}
Does anything seem out of place to anyone?
Also, In the PageController.h I added: -(void)runClick;
Here's the Error I catch:
2012-03-17 16:04:30.077 Blah[510:207] -[__NSCFString runClick]: unrecognized selector sent to instance 0x6a0b4f0

About the difference between loadView and viewDidLoad:
According to Apple, the controller (not you!) calls this method when it's view is requested and it's nil. So, it will create and load the view.
You can override loadView to create your own view (if you don't have a nib for that controller)
If you want to add something to the view created by a view controller with a nib file do it on viewDidLoad, and remove it on viewDidUnload.
About the error, can you provide more information? Are you debugging? What's the line it crashes?

Did you declare -(void)runClick in your .h file? (obvious question).
Also, if you want to get access to the control's properties (say if it were a slider, etc.), you need to add the sender bit:
-(void)runClick (id)sender

I believe you need to add a colon after runClick as in:
action:#selector(runClick:)

Related

How do I find the reference/identifier of a button on the storyboard in xcode?

Thanks for reading; this is my problem, I'm trying to hook up code I have written to buttons which already exist in the storyboard but I can't find a reference or identifier for the button(s).
I want to be able to modify buttons which have been created on the storyboard and style/give actions in my code.
So I would like something I can reference like so:
UIButton *subBtn;
where subBtn is the button's reference/identifier.
tldr; where do you find the reference for buttons in xcode (v6+) storyboard?
For those who come after me:
UIButton *subBtn = (UIButton *) [self.view viewWithTag:9];
The Line above allows you to reference a button with an "tag" which you can find in the attributes tab in the storyboard.
By using this it allows you to change attributes like below, to format your buttons:
[subBtn addTarget:self action:#selector(submitHit) forControlEvents:UIControlEventTouchUpInside];
UIImage *UISubImg = [UIImage imageNamed: #"submit_button.png"];
UIImage *UISubImg2 = [UIImage imageNamed: #"submit-play-icon.png"];
[subBtn setTitle:#"SUBMIT" forState:UIControlStateNormal];
subBtn.frame = CGRectMake(20, 400, 280, 65);
[self.view addSubview:subBtn];
[subBtn setBackgroundImage:UISubImg forState:UIControlStateNormal];
[subBtn setImage:UISubImg2 forState:UIControlStateNormal];
SubmitHit is a method.

UIButton event is not firing, even if I connect it through xib

I am creating a calculator game, in which I have to popup a UIAlertView, I customized the UIAlertView and put a UITableView programatically into it, When the user tap any of its cell I show a UIView on the top of UITableView, this view has a button, when I clicked that button the event does not fires, I have also installed it manually, but it doesn't work, previously the attachment of event with button is generating an error, for that I uninstalled the event in dealloc, and fix the problem, but after it still the event is not firing.
i was have case such your and i have to add it pragmatically
in .h declare
UIButton *btn;
in .m add it to the UIView
UIView *TempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
if (btn) {
[btn removeFromSuperview];
// [btn release]; // for not using arc
}
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 10, 25, 25)];
[btn addTarget:self action:#selector(ClickMe:) forControlEvents:UIControlEventTouchUpInside];
[TempView addSubview:btn];
[self.view addSubview:TempView];
and the action is
-(void)ClickMe:(id)sender{
//here is an action
}
it's working for me

How to add custom NSView to Window

I know how to do this in iOS but cannot figure it how to it in Cocoa.
I want to capture keyboard events and I think I need to override the acceptsFirstResponder method to accomplish that (keyDown method being triggered). So I created a class extending NSCustomView and tried to add it in the main Window but I just cannot understand how to do it. So far I added a Custom View to the main View then tried to add it programmatically like:
TestView *view = [[TestView alloc] init];
[[_window contentView] addSubview:view];
but this is not working. So how can I do this?
To see if the view has been added to a window, you can override the view's viewDidMoveToWindow method and log the value of [self window] to check (if it's nil then the view has been removed from a window):
- (void)viewDidMoveToWindow
{
NSLog(#"window=%p", [self window]);
[super viewDidMoveToWindow];
}
You should be subclassing NSView, not NSCustomView, and initWithFrame is the designated initializer for NSView, not init.
Try:
TestView *view = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
[[_window contentView] addSubview:view];

UI Element setAction and setTarget not working

I found some answers to my question but it still does not work. I have setup a button in IB and I want to setup the action and target by code(just for learning). I have a function in my MainViewController:
- (IBAction) onSpecialCase:(id)sender {
NSLog(#"It's working");
}
In the viewDidLoad function I do this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[myButton setTarget:self];
[myButton setAction:#selector(onSpecialCase:)];
}
Xcode already marks these lines telling me: "UIButton may not respond to 'setTarget'". When running the app anyways it terminates saying:
-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30
2011-06-29 08:12:22.465 FirstTry[3765:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30'
That's what I could expect from the XCode warning, but why is that? Every example I find on the web is exactly like mine. Adding the actionMethode to the button via IB works like expected. What am I doing wrong?
first check if you have declared the button as an outlet, second you should check if you connect the outlet to the button or use what I usually do is to add new button in code
UIButton *Button=[UIButton buttonWithType:UIButtonTypeCustom] ;
Button.frame=CGRectMake(0,0,30,30); // place your button
[Button addTarget:self action:#selector(processButtonCLick) forControlEvents:UIControlEventTouchUpInside];
[Button setImage:[UIImage imageNamed:#"imageoricon.png"] forState:UIControlStateNormal];
[self.view addSubview:Button];
It may help you.
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[sendButton setTitle:#"Send" forState:UIControlStateNormal];
[sendButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
sendButton.frame = CGRectMake(0, 3, 67, 37);
[sendButton addTarget:self action:#selector(sendButtonClicked) forControlEvents:UIControlEventTouchUpInside];

Xcode - UILabel is not changing value on viewDidAppear

Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.
Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.
My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.
I have tried everything to try and remove the subviews such as:
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
AND
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(
Any direction to sites that might help that you know of will be greatly appreciated.
Thanks!
Here is what I have in my controller that loads all the scrollview and labels:
- (void)viewDidAppear:(BOOL)animated {
// Create the scroll view for this view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 270);
[scrollView setFrame:CGRectMake(0, 198, 320, 170)];
// Do the labels for the text
UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont boldSystemFontOfSize:12];
dates.text = round_date;
dates.tag = 1;
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(10, 10, 280, 22)];
[scrollView addSubview:dates];
[dates release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}
Ok, the reason for this is that viewDidAppear will get called every time you present the view.
If you do this is
-(void) viewDidLoad;
instead.
To make sure you can access them later you might want to keep a reference to them in your UIViewController
-(void) viewDidLoad {
scrollView = [[UIScrollView alloc] initWithFrame];
[self.view addSubview:scrollView];
}
and define it in your header file with:
UIScrollView *scrollView;

Resources