I have a button on a view which when pressed opens another second view. I want to set a label in the second view to change depending on the label of the button that was pressed. I have tried passing (id) sender in my switchViews function but that does not work. Please help!
On your second view controller, create a the UILabel as a property i.e.
#interface MyViewcontroller : UIViewController {
UILabel *titleLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *titleLabel;
and, in interface builder, attach it to the label that you want to change.
Then, in your first view controller's switchViews method, after you create your second view you can set the title like this :
...
MyViewController *newViewController = [[MyViewController alloc] initWithNibName:'something' bundle:nil];
newViewController.view.titleLabel.text = #"Your new title goes here";
...
Hope that helps.
Passing sender should work. Just cast sender to UIButton * and take title for the state with titleForState:. Here is working code:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window makeKeyAndVisible];
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100, 100, 100,80);
[myButton setTitle:#"test title" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:myButton];
}
- (void)myButtonClicked:(id)sender{
UIButton * clickedButton = (UIButton *)sender;
NSString * buttonTitle = [clickedButton titleForState:UIControlStateNormal];
NSLog(#"title: %#",buttonTitle);
UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100,80)];
myLabel.text = buttonTitle;
[window addSubview:myLabel];
}
Related
I created a sub-class of UIViewController called toolbarViewController , I declare a UIButton in this view controller and specified it's target as following
UIButton *button = [[UIButton alloc]initWithFrame:myFrame];
[button addTarget:self action:#selector(doSomething) forContorlEvents:UIControlEventTouchUpInside];
then i do the following in a different view controller
toolbarViewController *toolbar = [[toolbarViewController alloc]init];
[self.view addSubview:toolbar.view];
the problem is that when i press the button i get the exception: Unrecognized selector ( doSomething ) send to instance , what am i doing wrong here ?
doSomething declaration in toolbarViewController.h
-(void)doSomething;
and in toolbarViewController.m
-(void)doSomething{ NSLog("doSomething got called"); }
Check the declaration of doSomething. If you declare IBAction something like -(IBAction)doSomething:(id)sender, you should add selector like
[button addTarget:self action:#selector(doSomething:) forContorlEvents:UIControlEventTouchUpInside];
instead of
[button addTarget:self action:#selector(doSomething) forContorlEvents:UIControlEventTouchUpInside];
if you use ARC,you alloc toolbarViewController,but it has relese。you can try to this:
ViewController.h
#interface ViewController : UIViewController{
toolbarViewController * toolbar;
}
#property(nonatomic,strong) toolbarViewController * toolbar;
ViewController.m
#synthesize toolbar = _toolbar;
toolbar = [[toolbarViewController alloc]initWithNibName:#"toolbarViewController" bundle:[NSBundle mainBundle]];
and you alloc button can do this:
UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bt.frame = myFrame;
[bt addTarget:self action:#selector(dosomething) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt];
One possible case is if you add a UITapGestureRecognizer to the UIButton's superview.
On iOS5 it seems there is an issue with UITapGestureRecognizers that do not cancel the tap in view.
So all you need to do is set the
tapGesture.cancelsTouchesInView = NO;
let me know if it helped!
so guys, i've been wondering, currently i'm in the middle of learning on developing apps. i saw a CNBC apps at ipad that looks like the image here : (sorry, new user cant directly post image D:)
http://images.thoughtsmedia.com/resizer/thumbs/size/600/at/auto/1291813093.usr105634.jpg
my question is, what are those 2 bars on top of the app??(the one with markets, and indexes)
is it a tabbar controller?? if it is how do we put it on top of the app instead of at the bottom like it normally is, and how do we have another tabbar inside a tabbar???
i appreciate your helps, and sorry for my bad english :3
okay, i have found the solution to this, by far i've tried both customized tabbar and segmented controller, but i found both of them risky and too complicated
so i do a little experiment with simple button
here's the main idea
first, i set up a toolbar, and give it a background
-in viewController.h
//adding my viewcontrollers
#class notLoggedHome;
#class LoggedInHome;
#class NABViewController;
//defining all the objects
#properties (nonatomic, strong) UIToolBar *mainToolBar;
#properties (nonatomic, strong) UIButton *toolBarBut1, *toolBarBut2, *toolBarBut3;
#properties (nonatomic, strong) UIImageView *logoImage;
#property (nonatomic, strong) notLoggedHome *viewNotLoggedHome;
#property (nonatomic, strong) LoggedInHome *viewLoggedInHome;
#property (nonatomic, strong) NABViewController *viewNAB;
#properties NSInteger lastTag;
-in viewController.m
#synthesize mainToolBar, toolBarBut1, toolBarBut2, toolBarBut3;
#synthesize logoImage, lastTag;
#synthesize viewNotLoggedHome, viewLoggedInHome, viewNAB;
-(void)viewDidLoad
{
lastTag = 100;
self.view.backgroundColor = [UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1];
//---
//---fakeTabBar set up===
viewNotLoggedHome = [[notLoggedHome alloc]init];
viewLoggedInHome = [[LoggedInHome alloc]init];
viewNAB = [[NABViewController alloc]init];
//creating the fakeTabBar
mainToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
[mainToolBar setBackgroundImage:[UIImage imageNamed:#"menu_bar.jpg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
//defining images
imgHome = [UIImage imageNamed:#"menu_home.png"];
imgHomeS = [UIImage imageNamed:#"menu_home_s.png"];
imgLogo = [UIImage imageNamed:#"menu_bar_logo_ep.png"];
UIImageView *logoImage = [[UIImageView alloc]initWithImage:imgLogo];
logoImage.frame = CGRectMake(0, 0, imgLogo.size.width, imgLogo.size.height);
//--button setting====
toolBarBut1 = [UIButton buttonWithType:UIButtonTypeInfoLight];
[toolBarBut1 setFrame:CGRectMake(imgLogo.size.width, 1, imgHome.size.width, imgHome.size.height)];
toolBarBut1.tag = 0;
toolBarBut1.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
[toolBarBut1 setImage:imgHome forState:UIControlStateNormal];
[toolBarBut1 setImage:imgHomeS forState:UIControlStateSelected];
[toolBarBut1 addTarget:self action:#selector(barPressed:) forControlEvents:UIControlEventTouchUpInside];
//do the same with the other 2 button
//---------------------
[mainToolBar addSubview:logoImage];
[mainToolBar addSubview:toolBarBut1];
//do the same with the other 2 button
[self.view addSubview:mainToolBar];
[super viewDidLoad];
}
-(void)barPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
if(button.tag == 0 && button.tag != lastTag)
{
[viewNAB removeFromParentViewController];
[viewNotLoggedHome removeFromParentViewController];
[self.view addSubview:viewLoggedInHome.view];
button.selected = YES;
}
if(button.tag == 1 && button.tag != lastTag)
{
[viewNAB removeFromParentViewController];
[viewLoggedInHome removeFromParentViewController];
[self.view addSubview:viewNotLoggedHome.view];
button.selected = YES;
}
if(button.tag == 2 && button.tag != lastTag)
{
[viewLoggedInHome removeFromParentViewController];
[viewNotLoggedHome removeFromParentViewController];
[self.view addSubview:viewLoggedInHome.view];
button.selected = YES;
}
lastTag = button.tag;
}
so the main idea is creating a fake tabbar by using toolbar, assigning UIButton(s) to the toolbar as the fake tabbaritem, and giving mechanism to each button that later will switch your viewcontrollers (you have to alloc the viewcontrollers first at implementation file)
this works well for me, just dont forget to set the view controllers frame Y point +(toolbar Height) because otherwise it will cover the toolbar later
:)
Anywhere there is a UIButton in my app i would like it to have a specific background image. This background image will always be the same.
What i dont want to have to do is call a method on every button that i put into my UI.
Can i do this via a category or something similar?
You can't change global appearance of your button via simple code (in current SDK).
BUT, You can subclass your UIButton. For example:
.h
#interface MyButton : UIButton
#end
---
.m
#implementation MyButton
// your customization code
#end
And if you want to insert an UIButton instance like:
UIButton *button = // init your button
// customize your button
[self.view addSubview:button];
you have to change UIButton to MyButton
Mybutton *button= // init your button
[self.view addSubview:button];
Don't forget about #import "MyButton.h" in your .m / .h file.
EDIT: Where should you make customizations:
#implementation UIButton (MyCategory)
+ (id)buttonWithFrame:(CGRect)frame {
return [[[self alloc] initWithFrame:frame] autorelease];
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// HERE YOU CAN DO SOME CUSTOMIZATION
}
return self;
}
Then, somewhere in your viewController:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
or:
UIButton *btn = [UIButton buttonWithFrame:CGRectMake(x, y, width, height)];
I have a scroll view; inside I add one or more text views; to each text view I add a button:
UIScrollView
UITexView
UIButton
UITextView
UIButton
...
This is part of the code:
- (void)viewDidLoad {
...
[self loadUI];
...
}
-(void) loadUI {
UITextView *textView;
...
for (...) {
UIButton *editButton = [[UIButton alloc] initWithFrame:
CGRectMake(self.scrollView.frame.size.width - 230, 0, 50, 20)];
...
[editButton addTarget:self action:#selector(editPressed:)
forControlEvents:UIControlEventTouchUpInside];
...
textView = [[CustomTextView alloc] initWithFrame:
CGRectMake(10, dynamicHeight, self.queuedView.frame.size.width - 100, 500)];
textView.userInteractionEnabled = NO;
...
[textView addSubview:editButton];
[editButton release];
...
[self.scrollView addSubview:textView];
[textView release];
}
}
- (IBAction) editPressed:(id)sender {
...
}
For some reason, the editPressed method is not called when I press the button. Any ideas why? I tried setting the interaction enabled for the text view to YES, but still nothing. I changed form (IBAction) to (void), still nothing.
Thank you,
Mihai
Apparently, textView.userInteractionEnabled = YES; did the trick. I tried this before but it did not work. Maybe I had to do a clean build after the change.
Try adding the button to the rightView of the textfield
dotextField.rightView = editButton instead of [textView addSubview:editButton];
Is it possible to allow user input text on a UIImageView, just like the text tool in painter?
I cannot find any resource on this topic?
UIImageView is not designed to hold any text, but you could add a UILabel or UITextField either within it or on top / below it, depending on what you want to do.
For example, suppose you want to allow the user to edit a piece of text inside an image. You could do something like this:
UIImage* image = [UIImage imageNamed:#"my_image.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = YES;
UITextField* textField = [[UITextField alloc]
initWithFrame:CGRectMake(10, 10, 50, 20)];
textField.placeholder = #"type here";
[imageView addSubview:textField];
// You might also want to set the imageView's frame.
[self.view addSubview:imageView];
If you add a UITextField as a subview of a UIImageView, it's important to set the userInteractionEnabled to YES, since it defaults to NO for that superview (it's usually YES by default in most UIViews).
Addendum
If you want the user to be able to click anywhere in the image to edit the text, here is one way to do it: subclass UIControl and add a UIImageView and a UITextField as subviews of it, and connect the clicking action of the UIControl to the UITextField. Something like this (WARNING: not tested code, but it conveys the general idea):
#interface ImageAndTextView : UIControl {
UIImageView* imageView;
UITextField* textField;
}
#property (nonatomic, retain) UIImageView* imageView;
#property (nonatomic, retain) UITextField* textField;
- (void) click;
#end
#implementation ImageAndTextView
#synthesize imageView, textField;
- (id) initWithFrame: (CGRect) frame_ {
if (self = [super initWithFrame:frame_]) {
UIImage* image = [UIImage imageNamed:#"my_image.png"];
self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
CGRect textFrame = CGRectMake(10, 10, 50, 20); // whatever frame you want
self.textField = [[[UITextField alloc]
initWithFrame:textFrame] autorelease];
[self addSubview:textField];
// Now register an event to happen if the user clicks anywhere.
[self addTarget:self action:#selector(click)
forEvent:UIControlEventTouchUpInside];
}
return self;
}
- (void) click {
[textField becomeFirstResponder];
}
#end
This is not possible with the current iPhone API (3.1). You will need to create your own custom uitextfields and your own image rendering methods to combine the layers into a captioned photo.