Undeclared identifier "viewDidLoad" - ios

//Objects list
//objects scroll
UIScrollView *objects;
objects=[[UIScrollView alloc]initWithFrame:CGRectMake(0,80,512,688)];
objects.showsVerticalScrollIndicator=YES;
objects.scrollEnabled=YES;
objects.userInteractionEnabled=YES;
objects.backgroundColor = [UIColor clearColor];
[self.view addSubview:objects];
[objects setUserInteractionEnabled:YES];
[objects setCanCancelContentTouches:YES];
objects.contentSize = CGSizeMake(512,689);
//divider
UIButton *divider = [UIButton buttonWithType:UIButtonTypeCustom];
[divider setBackgroundImage:[UIImage imageNamed:#"white.png"]forState:UIControlStateNormal];
divider.frame = CGRectMake(300, 0, 1, 768);
[divider setTitle:#"" forState:UIControlStateNormal];
[self.view addSubview:divider];
divider.adjustsImageWhenHighlighted = NO;
//array colors
UIImage *color[3000];
//color setup
color[0] = [UIImage imageNamed:#"item.png"];
UIImageView *originalImageView = [[UIImageView alloc] initWithImage:color[0]];
[originalImageView setFrame:CGRectMake(300, 0, 212, 62)];
[objects addSubview:originalImageView];
//array buttons
UIImageView *button[3000];
//button setup
button[0] = [[UIView alloc] initWithFrame:[originalImageView frame]];
UIImageView *maskImageView = [[UIImageView alloc] initWithImage:color[0]];
[maskImageView setFrame:[button[0] bounds]];
[[button[0] layer] setMask:[maskImageView layer]];
button[0].backgroundColor = [UIColor blueColor];
[objects addSubview:button[0]];
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the updated code, some before, and everything after. There's a lot more to the program but it is all just setting up different visual elements for a GUI. The error is tagged on the line that sais..
-(void)viewDidLoad {
And the error sais this..
Use of undeclared identifier 'viewDidLoad'
Would you like more info??

you cannot have method inside another method.
this is part of your code
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}
} // <----this should not be here
change it to
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
} // <--- should be here to end method
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}

Related

UINavigationController centered button using obj-c

I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}

How to setting NavigationBar's barButtonItems

Now,I found the problem maybe from my custom button--DJExchangeImageTitleButton.This is the code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
frame.size.width += DJMargin;
[super setFrame:frame];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 如果仅仅是调整按钮内部titleLabel和imageView的位置,那么在layoutSubviews中单独设置位置即可
// 1.计算titleLabel的frame
self.titleLabel.x = self.imageView.x;
// 2.计算imageView的frame
self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + DJMargin;
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];
// 只要修改了文字,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[super setImage:image forState:state];
// 只要修改了图片,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
-(void)setHighlighted:(BOOL)highlighted{};
Who can tell me what's wrong with my customed button?I will appreciate a lot.
When I set navigationBar's left,right and titleView,and it's normal;
However,every times,I push to another controller,and I pop back,the left and right barButtonItems become longer and longer,the titleView is shorter and shorter.
This is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupNaviBar];}
-(void)setupNaviBar{
//leftButton
DJExchangeImageTitleButton *titleBtn = [[DJExchangeImageTitleButton alloc]init];
[titleBtn setImage:[UIImage imageNamed:#"homgpage_navi_left"] forState:UIControlStateNormal];
[titleBtn setTitle:#"深圳" forState:UIControlStateNormal];
titleBtn.backgroundColor = [UIColor redColor];
titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
titleBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:titleBtn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = #[negativeSpacer,leftItem];
//searchBar
UIButton *btn = [[UIButton alloc]init];
btn.backgroundColor = [UIColor whiteColor];
btn.frame = CGRectMake(0, 0, 250, 25);
btn.layer.cornerRadius = 3;
[btn setImage:[UIImage imageNamed:#"search_icon"] forState:UIControlStateNormal];
[btn setTitle:#"请输入要搜索的职位名称" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.imageEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
[btn addTarget:self action:#selector(goSearch) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = btn;
//rightButton
DJExchangeImageTitleButton *notiBtn = [[DJExchangeImageTitleButton alloc]init];
notiBtn.backgroundColor = [UIColor redColor];
[notiBtn setTitle:#"通知" forState:UIControlStateNormal];
[notiBtn setImage:[UIImage imageNamed:#"homgpage_navi_right"] forState:UIControlStateNormal];
notiBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
notiBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:notiBtn];
self.navigationItem.rightBarButtonItem = right;
}
How can I solve this problem?Sorry I can't post a image to show my question.

Adding two button via coding in viewDidLoad section is always hiding one button

Adding two button via coding in viewDidLoad section is always hiding one button, and I don't know why this is happening.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIImage *addButton=[UIImage imageNamed:#"add.png"];
add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[add setImage:addButton forState:UIControlStateNormal];
[self.view addSubview:add];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[add setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
This looks like a copy and paste error. You never set the frame, image, or target action for the second button because you have "add" where you should have "cntctBtn".
- (void)viewDidLoad {
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addButton.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[addButton addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[addButton setImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
[self.view addSubview: addButton];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cntctBtn.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[cntctBtn addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[cntctBtn setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
Its a copy paste error. Above is the corrected code.

Making series of buttons into an array ios

I have about 10-12 buttons that I'm adding to my scrollview. How can I make these into an array of buttons so that I can simplify the code? As of right now my code(only first three buttons are shown) is as follows:
UIButton *redButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
redButton.frame = CGRectMake(0, 0, 50, 30);
redButton.tag = 2;
[redButton setTitle:#"red" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
[redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[redButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:redButton];
UIButton *blueButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
blueButton.frame = CGRectMake(70, 0, 50, 30);
blueButton.tag = 3;
[blueButton setTitle:#"blue" forState:UIControlStateNormal];
blueButton.backgroundColor = [UIColor blueColor];
[blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[blueButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:blueButton];
UIButton *greenButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
greenButton.frame = CGRectMake(140, 0, 50, 30);
greenButton.tag = 5 ;
[greenButton setTitle:#"green" forState:UIControlStateNormal];
greenButton.backgroundColor = [UIColor greenColor];
[greenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[greenButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:greenButton];
...
Can you see if this is possible
- (void)addButtonsToScrollView
{
NSArray *buttons = #[#{#"Tag":#2,#"Title":#"red",#"Color":[UIColor redColor]},
#{#"Tag":#3,#"Title":#"blue",#"Color":[UIColor blueColor]},
#{#"Tag":#5,#"Title":#"green",#"Color":[UIColor greenColor]}];
CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f);
for (NSDictionary *dict in buttons)
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
button.tag = [dict[#"Tag"] integerValue];
[button setTitle:dict[#"Title"]
forState:UIControlStateNormal];
button.backgroundColor = dict[#"Color"];
[button setTitleColor:[UIColor blackColor]
forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
frame.origin.x+=frame.size.width+20.0f;
}
CGSize contentSize = self.scrollView.frame.size;
contentSize.width = frame.origin.x;
self.scrollView.contentSize = contentSize;
}
You can use:
[NSArray arrayWithObjects:redButton,greenButton,blueButton,nil];
But might be better off using a NSDictionary
[NSDictionary dictionaryWithObjectsAndKeys:
redButton, #"red",
blueButton, #"blue",
greenButton, #"green",
nil];
That way you can use the key to look them up instead of index.
Ok, we have the solution, try this code mate,
-(void) createButtons{
NSDictionary *buttonColors = #{#"Red":[UIColor redColor],#"Green":[UIColor greenColor],#"Black":[UIColor blackColor],#"Yellow":[UIColor yellowColor],#"Blue":[UIColor blueColor]};
int tag = 1;
for(NSString *key in buttonColors.allKeys){
UIColor *color = [buttonColors objectForKey:key];
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(((tag -1)*70), 0, 50, 30);
[button setFrame:frame];
button.tag = tag;
button.backgroundColor = color;
[button setTitleColor:color forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:key forState:UIControlStateNormal];
[self.scrollView addSubview:button];
tag++;
}
}

iOS Subview with buttons not responding to touches.

I have a UIView Subclass that has a frame/image with buttons in it. I'm trying to add it to my view controller however when I do it shows up but none of the buttons will register ANY touches.
Here is my UIView Subclass.
#import "ControlView.h"
#implementation ControlView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect slideFrame = CGRectMake(0, 402, 320, 82);
slider = [[UIView alloc] initWithFrame:slideFrame];
slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"shelf.png"]];
[self addSubview:slider];
UIImage *btnImage = [UIImage imageNamed:#"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btnImage = [UIImage imageNamed:#"NavBar_01.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(12, 28, 70, 50);
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn1];
[slider bringSubviewToFront:btn1];
[btn1 addTarget:self action:#selector(home) forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_02.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_02_s.png"];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(87, 28, 70, 50);
[btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn2];
[slider bringSubviewToFront:btn2];
// [btn2 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_03.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_03_s.png"];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(162, 28, 70, 50);
[btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn3];
[slider bringSubviewToFront:btn3];
// [btn3 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_04.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_04_s.png"];
btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
btn4.frame = CGRectMake(237, 28, 70, 50);
[btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn4];
[slider bringSubviewToFront:btn4];
// [btn4 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideUp)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self addGestureRecognizer:recognizer];
}
return self;
}
-(void)slideUp
{
// Slide up based on y axis
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 320;
slider.frame = frame;
[UIView commitAnimations];
}
-(void)slideDown
{
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 425;
slider.frame = frame;
[UIView commitAnimations];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
And this is how I'm adding it to my ViewController.
ControlView *cont = [[ControlView alloc]init];
[homeView.view addSubview:cont];
I tried adding the same code to a ViewController and the buttons etc.. worked so I'm sure this is something simple/stupid with Delegates/Self etc.. between the subview and the ViewController however I have a mental block.
You are doing this?
ControlView *cont = [[ControlView alloc]init];
you are supposed to do
ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];

Resources