Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
There are a lot of similar questions and answers, but they don't solve the main problem: how to properly add any text field in cocos2d with its workable delegate.
For example if I create UITextfield and simply add it to [[CCDirector sharedDirector] view] then it will call textFieldShouldReturn: method only. No more methods doesn't work, even textViewDidBeginEditing:
I gave the same result with using of separate view and UITextField. I also tried to create a separate UIViewController to use its view with UITextField but it even make my app falling down
1: Make sure your CCNode contains the UITextFieldDelegate:
#interface MyLayer : CCLayer <UITextFieldDelegate>
2: Init the UITextField as per usual:
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, fieldWidth, fieldHeight)];
nameField.textAlignment = NSTextAlignmentCenter;
nameField.font = [UIFont fontWithName:kFontName size:24];
nameField.text = #"Enter Name";
nameField.delegate = self;
nameField.backgroundColor = [UIColor grayColor];
nameField.textColor = [UIColor whiteColor];
nameField.autocorrectionType = UITextAutocorrectionTypeNo;
3: Add the field to the CCDirector view:
[[[CCDirector sharedDirector] view] addSubview:nameField];
4: Implement the UITextField delegates in your CCNode:
- (BOOL)textFieldShouldReturn:(UITextField*)textField
- (void)textFieldDidBeginEditing:(UITextField *)textField
My mistake was that UITextFieldDelegate and UITextViewDelegate have some similar mehods, and I used some methods from the second delegate. Example:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
Of course I wrote (UITextView *)textField at the end.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to make a simple ViewController with two or three Buttons and labels. When I press the Button it plays an audio file.
I want to make hundreds of this similar screen, what is the best way to create it? (I am currently creating it with MainStoryBoard.)
For each page I would like to make small changes such as Buttons sizes ,Buttons numbers, label texts and audio files.
Drawing text bubbles or using xib file might be good, but I am not sure what I should.
You first need to have your own class for this UIViewController as a base class that has buttons, labels, etc.
Then use Factory Design pattern to generate the inherited UIViewController class which allows you to do some tweak that fits you need.
Factory Design Pattern
BaseViewController {
UIButton *button1;
UIButton *button1;
UILabel *label1;
}
ChildViewControllerA : BaseViewController {
UIButton *button3
}
ChildViewControllerB : BaseViewController {
UIButton *button4
}
Factory : NSObject {
+ (BaseViewController)generateChildViewController: (int) type {
switch (type)
case 0:
return [[ChildViewControllerA alloc] init];
case 1:
return [[ChildViewControllerB alloc] init];
}
}
Main {
- (void)createThreeViewControllers {
BaseViewController *vc1 = [Factory generateChildViewController:0];
vc1.button1.backgroundColor = [UIColor blueColor];
BaseViewController *vc2 = [Factory generateChildViewController:0];
vc2.button2.center = cgpointmake (100, 150);
BaseViewController *vc3 = [Factory generateChildViewController:0];
vc3.label1.text = #"vc3 test";
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a subclass of UIView like so:
#implementation AView
- (UIView *) buildBestView
{
UIView *test = [[UIView alloc] init];
return test;
}
#end
If I include AView.h in my controller, how do I call this method?
Thanks.
Declare your method in your Interface
#interface AVView : UIView
- (UIView *) buildBestView;
#end
Use that in the other class
AVView *avView = [[AVView alloc] initWithFrame:CGRectMake:(0,0, 100, 100)];
[avView buildBestView];
I would recommend you creating a class method instead like this:
+ (UIView *) buildBestView // Note the "+" instead of the "-"
{
UIView *test = [[UIView alloc] init];
return test;
}
Then you can call it like this
AView *aview = [AView buildBestView];
// Do whatever you need with aview
This question already has an answer here:
resignFirstResponder for all textfields [duplicate]
(1 answer)
Closed 8 years ago.
I know that I can resign a single text field to first responder when touching a button by implementing the following:
- (IBAction)signMeUpButton:(id)sender {
[self.textfield resignFirstResponder];
}
However, In my case I have multiple text fields, and I feel like typing them in one at a time, cant possibly be the best way / most modern way of doing so.
I reviewed multiple questions on the site, such as this one:
iOS SDK: Dismiss keyboard when a button gets clicked?
But none of them mention, having more than one text field and dismissing them with an IBAction ... What can I do in this situation?
You may try this:
- (IBAction)signMeUpButton:(id)sender
{
[self.view endEditing:YES];
}
If you have more than one text field, it's best to implement the following
[self.view endEditing:YES];
So, in your case it would be :
- (IBAction) signMeUpButton:(id)sender {
[self.view endEditing:YES];
}
That will do exactly what you are looking for!
try this
UITextField *currentTextField;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
-(IBAction)click:(id)sender
{
[currentTextField resignFirstResponder];
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Would someone be so kind as to point out where this tutorial is steering me wrong?
Open up the -SECOND- project you made and change the name of:
RootViewController.h
RootViewController.mm
To:
SecondView.h
SecondView.mm
Now, open up SecondView.h and change the following code:
#interface RootViewController: UIViewController {
}
#end
To:
#interface SecondView: UIViewController {
}
#end
Save and close SecondView.h and open up SecondView.mm. In SecondView.mm, let's change the following code:
#import "RootViewController.h"
To:
#import "SecondView.h"
And change the following code:
#implementation RootViewController
To:
#implementation SecondView
Part 2: Set up the view controller
Save and close SecondView.mm. In iFile, cut both SecondView.h and SecondView.mm and paste them into the testview project folder. Open your Makefile, and on the line:
testview_FILES = main.m testviewApplication.mm
Edit that line to look like:
testview_FILES = main.m testviewApplication.mm SecondView.mm
This will compile SecondView.mm into an object file (SecondView.o) and add it to the finalized testview binary (testview.app > testview). And guess what? That's how you set up a new class for your projects! Proceed to part 3!
Part 3: Set it up in-app (Usage)
So you have a new UIViewController and UIView ready to be used whenever you want huh? Well now we will call our view controller to present our amazingness view! With the shiny code:
//Bring SecondView to memory (RAM)
SecondView *second= [[[SecondView alloc] init] autorelease];
//Properties for second (SecondView)
second.title = #"UINavigationBar";
//Present second (SecondView) to user
[self presentModalViewController:second animated:YES];
Now that you have the ability to bring another view up front, a question you want to ask yourself is whether or not you want the user to be able to go back to the RootViewController. If not, then just skip this step. Using the action property of either UIButtons or UIBarButtonItems to UIAlertViews or UIActionSheets, you can call this whenever needed.
The problem is that my build is throwing an error of "SecondView was not declared in this scope" and "second was not declared here". Every other part of this tutorial has worked perfectly so I am assuming there's a typo here in Step 3 just above the code. I'm just jumping into apps developing and do not have the luxury of Xcode or many tutorials without Xcode. EVERYTHING HAS BEEN FOLLOWED TO THE LETTER. Thanks.
In your RootViewController.mm file
#import "RootViewController.h"
#import <UIKit/UIKit.h>
#import "SecondView.h"
#implementation RootViewController
- (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor redColor];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(21, 80, 100, 35);
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
- (void)myButtonPressed {
SecondView *second= [[[SecondView alloc] init] autorelease];
second.title = #"UINavigationBar";
[self presentModalViewController:second animated:YES];
}
#end
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In an iOS view controller I typically have code like:
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
// [several lines of code to configure the view]
[self.view addSubview:scrollView]
}
This tends to get cluttered so I add a helper method (e.g. createScrollView) to alloc, init, and configure the view. Is this an established pattern for building views and is there a convention for naming the helper methods? One thing I noticed is that the name initScrollView is not allowed because of ARC.
Here's the pattern I follow:
In init / initWithFrame: create your objects, and set any properties which will never change during the life of this controller:
- (instancetype) init {
self = [super init];
if (self) {
_textField = [UITextField new];
_textField.keyboardType = UIKeyboardTypeEmailAddress;
}
return self;
}
If you want to separate these out into methods like createTextFields, etc., that's fine, although it's easier to debug if you can see a list of everything instantiated in one place.
In viewDidLoad, set up the view hierarchy:
- (void) viewDidLoad {
[self.view addSubview:self.textField];
}
In viewWillLayoutSubviews, set the frames (if you're not using auto-layout):
- (void) viewWillLayoutSubviews {
self.textField.frame = CGRectMake(10, 44, 320, 50);
}
This approach will set you up for success handling view resizing and rotation events.