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";
}
}
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 8 years ago.
Improve this question
This might be simple, but I'm very new at developing an iOS APP. I have an UIButton and three UIImage's. What I'm trying to do is that when the user taps the button, the first image will display, if the user taps it again, then show the second image, and so on. When the last image is displaying, then go back to the first one when tapping the button again. The problem is that only the first image appears.
Here is my code:
file.h
#interface ViewController : UIViewController
{
IBOutlet UIImageView *images;
}
-(IBAction)changeImage:(id)sender;
#end
file.m
#implementation ViewController
-(IBAction)changeImage:(id)sender{
images.image = [UIImage imageNamed:#"pic1.png"], [UIImage imageNamed:#"pic2.png"],[UIImage imageNamed:#"pic3.png"];
}
Any help would be greatly appreciated!
Than you in advance!
You can declare a global variable for the index and global variable array to hold the image names
self.index = 0;
self.imageList = [NSArray arrayWithObjects:#"pic1", #"pic2", #"pic3", nil];
then in the click action, you can do
-(IBAction)changeImage:(id)sender{
images.image = [UImage imageNamed:self.imageList[self.index]];
self.index = (self.index + 1 ) % [self.imageList count];
}
I don't have the compiler with me to verify the syntax, but you get the idea.
This is easy, create a counter called tapCounter in viewDidLoad. Let's imagine you already set your image array as follows in viewDidLoad. Make sure images and tapCounter are properties of your class
- (void)viewDidLoad {
[super viewDidLoad];
self.images = #[[UIImage imageNamed:#"pic1.png"], [UIImage imageNamed:#"pic2.png"],[UIImage imageNamed:#"pic3.png"]];
self.tapCounter = 0;
}
-(IBAction)changeImage:(id)sender {
UIButton *but = (UIButton *)sender;
tapCounter++;
if (tapCounter == 4) {
tapCounter = 1;
}
[but setBackgroundImage:[images objectAtIndex:tapCounter-1] forState:UIControlStateNormal];
}
}
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i created nine buttons using IBOutletCollection here.
My question is - i want to get title which button is pressed and display it on another buttons title. Is this possible!
Try like this,
- (IBAction)buttonAction:(UIButton *)btn {
yourNextVCobject.buttonTitle = [NSString stringWithFormat:#"%#",btn.titleLabel.text];
// then push to ur vc.
}
In your Next VC,
In .h
#property (nonatomic, retain) NSString *buttonTitle;
In .m
[yourAnotherBtn setTitle:self.buttonTitle forState:UIControlStateNormal];
You can use the below code to set the title of clicked button to targetted another button.
-(IBAction)clickbutton:(id)sender
{
UIButton *firstButton = (UIButton *)sender;
NSString *neededtitle = [NSString stringwithformat:#"%#",firstButton.currentTitle];
[anotherbutton setTitle:neededtitle forState:UIControlStateNormal];
}
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.
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.