What is the recommended way to factor iOS UI initialization? [closed] - ios

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.

Related

How to create many similar ViewControllers [closed]

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";
}
}

Creating a subclass of UIView [closed]

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

Window with a view controller [closed]

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 9 years ago.
Improve this question
Hi guys i am trying to make a window with a view controller (which i guess is like GroupBox in .Net (fix me if i'm wrong..)) and i was trying to start the application on the simulator and it threw an exception. I am using storyboards.
#interface ViewController ()
#end
#implementation ViewController
-(id)init
{
self = [super init];
self.arSongsCollection = [[NSMutableArray alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self.arSongsCollection;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_arSongsCollection addObject:[[Song alloc] initWithTitle:#"Song" andArtist:#"Artist" andURL:[NSURL URLWithString:#"http://songurl.com/song.mp3"]]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Read more about tableview datasources, here's the problem:
_tableView.dataSource = self.arSongsCollection;
this ususally should also point to self
_tableView.dataSource = self;
and you have to implement both necessary tableview delegate and dataSource methods in the view controller class.

How can I invoke a method in a UIView subclass, everytime the device rotates? [closed]

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 9 years ago.
Improve this question
I want to call a function in a UIView subclass of an item added to the main window, when the device rotate.
I've many items so I don't want to call such method one by one, but I want it automatically invoked for this item and all subviews.
thanks
To get the automatic subview behavior, declare a UIView category adding the method you wish to be invoked. Then have your view subscribe to device rotation notifications, and as part of its handling of the notification, call that same method on all its subviews. Something like this (typed without benefit of autocorrect or compiled, so please take as an illustration and not a full example program):
#interface UIView (MyAppAdditions)
- (void)rotationDetected;
#end
#implementation UIView (MyAppAdditions)
- (void)rotationDetected
{
}
#end
#implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
return self;
}
- (void)didRotate:(NSNotification *)notification
{
[self rotationDetected];
[self.subviews makeObjectsPerformSelector:#selector(rotationDetected)];
}
#end
A more flexible implementation would also override initWithCoder: or awakeFromNib to subscribe the rotation notification as well.

Add UITextFieldDelegate in cocos2d? [closed]

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.

Resources