Creating a subclass of UIView [closed] - ios

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

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

How to create a tip balloon? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my image.I want to create a tip balloon like this. This must visible when user types in textfield
I have created one more but that is in cloud shape. Please help to create this
Subclass the UITextField then override becomeFirstResponder (show bubble) and resignFirstResponder (hide bubble) and when the field is active present your bubble.
.h
#import <UIKit/UIKit.h>
#interface BubbleTextField : UITextField
#end
.m
#import "BubbleTextField.h"
#interface BubbleTextField ()
{
UIView *_bubbleView;
}
- (void)showBubble:(BOOL)show;
#end
#implementation BubbleTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialize the _bubbleView here
}
return self;
}
#pragma mark - Override
- (BOOL)becomeFirstResponder {
BOOL shouldBecome = [super becomeFirstResponder];
if (shouldBecome)
{
[self showBubble:YES];
}
return shouldBecome;
}
- (BOOL)resignFirstResponder {
BOOL shouldResign = [super resignFirstResponder];
if (shouldResign)
{
[self showBubble:NO];
}
return shouldResign;
}
#pragma mark - Private Methods
- (void)showBubble:(BOOL)show {
// Show/Hide
// Animations etc.
}
#end
Check out KBPopupBubble.
And CMPopTipView
Also, check out these search results for custom controls on cocoa controls. Search Results.

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

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.

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.

Viewcontrollers best practice [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am just about to start a new project where I have a custom menu that I need to display on everyview that I have. I dont want to use tab bars as this menu is custom designed and may have some animation added to it at some point.
Is there a simple way of creating this menu in one place so that I dont have to build it into every xib file??
Thanks
The tab bar controller is a system provided container controller. If you're using iOS 5 and later, you can make your own custom container view controller:
See Custom Container View Controllers discussion in the View Controller Programming Guide.
The key methods you need are enumerated in the UIViewController Class Reference, too.
I'd also suggest checking out WWDC 2011 #102 - Implementing UIViewController Containment.
Update:
If you want to write your own custom menu, you could do something like the following. I'm not doing anything fancy, but I'm just adding three colored subviews that might correspond to your custom buttons. And I have a tap gesture recognizer on each, which you can obviously handle as you see fit:
NSInteger const kHeight = 50;
NSInteger const kCount = 3;
#interface CustomMenu ()
#property (nonatomic, strong) NSMutableArray *menuViews;
#end
#implementation CustomMenu
- (id)init
{
self = [super init];
if (self)
{
_menuViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < kCount; i++)
{
UIView *subview = [[UIView alloc] init];
subview.tag = i;
[self addSubview:subview];
[_menuViews addObject:subview];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[subview addGestureRecognizer:recognizer];
}
[_menuViews[0] setBackgroundColor:[UIColor blueColor]];
[_menuViews[1] setBackgroundColor:[UIColor redColor]];
[_menuViews[2] setBackgroundColor:[UIColor greenColor]];
}
return self;
}
- (void)layoutSubviews
{
CGFloat width = self.superview.bounds.size.width;
CGFloat height = self.superview.bounds.size.height;
CGFloat menuChoiceWidth = width / kCount;
self.frame = CGRectMake(0, height - kHeight, width, kHeight);
NSInteger subviewIndex = 0;
for (UIView *subview in self.menuViews)
{
subview.frame = CGRectMake(subviewIndex * menuChoiceWidth, 0,
menuChoiceWidth, kHeight);
subviewIndex++;
}
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
NSLog(#"%s tapped on %d", __FUNCTION__, recognizer.view.tag);
}
#end
Then, you various view controllers just need to make sure to add the CustomMenu to the view:
#interface ViewController ()
#property (nonatomic, strong) CustomMenu *menu;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.menu = [[CustomMenu alloc] init];
[self.view addSubview:self.menu];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.menu layoutSubviews];
}
#end
I confess that I've given up on iOS 4.3 support (it just isn't worth the heartache and the size of the 4.3 audience is pretty small nowadays), so I don't deal with this silliness any more, but hopefully this gives you a sense of what one possible solution might look like.

Resources