Display some dynamic text on a UIButton - ios

My ViewController shows a button at the top.
The button is part of the view defined through Interface Builder and doesn't have any text on its label.
When the ViewController's view is shown, I set the text of the button in the following way:
[dButton setTitle:[NSLocalizedString( #"DeleteButton", #"" ) uppercaseString] forState: UIControlStateNormal];
dButton.titleLabel.textColor = [UIColor whiteColor];
The reason for doing this at runtime is to have the title taken from the appropriate internationalized strings file. The code above is invoked by the viewDidAppear:animated: method of my ViewController and it works as expected on a similar view.
The view where it doesn't work is the one where I show a UIImagePickerController upon loading so this might be the cause of my problem: does anybody know how to deal with it? I.e. How can I have the button shown with its text after the UIImagePickerController is closed?
-- EDIT
- (void)viewDidAppear:(BOOL)animated {
[self setupDeleteButton];
[super viewDidAppear:animated];
NSLog( #"Title is >%#<", dButton.titleLabel.text );
}

Solved!
The UIButton I was using had been set up on Interface Builder with a 'Custom' Type and with an image on the 'Image' property while I should have set the image on the 'Background' property: the text was actually added to the button, but the Image property was covering it!

Try setting the title in the viewWillAppear method.

Related

Change Background Image of an iOS app with a button click

Title pretty much says it all, I am new to Xcode and am trying to make a button that changed the view's background image. I will have three button and when you click one they will change the background that is associated with the button. How would I accomplish this? I have looked around but have not found anything relevant or anything that worked..
Thanks in advance!
EDIT::
As for what I have, I have a project set up with a window and one view with a background image called "default#2x-568h#2x.png" I would like to be able to press a button and have it switch to "second.png". I have a button and I control dragged into my .h which made this:
#interface TeslameterViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UIButton *button1;
}
- (IBAction)button1:(id)sender;
#end
thats where I am lost.
EDIT AGAIN:
Found the fix via this video:
http://www.youtube.com/watch?feature=player_embedded&v=iKuGiJMgMiQ
On the button Action event add this line.
(void)Action:(UIButton *)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"bg.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
try this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]];
and also see UIView Image Background for example project with complete code...
You need to be more specific on what you need.
Within your xib file, create a UIImageView and stretch its dimensions to fit the size of the screen. Also create a the necessary buttons. Create properties in your view controller to have a reference to the UIImageView and the buttons. Give the buttons an action that changes the picture that the UIImageView loads.
You have to create three buttons in nib file and add outlet to your view controller.
To create outlet you have right click on button then you will get list of event, then control drag to your view controller header file, then one small window will pop up, write method name and click enter. Now u will see method in your controller.write code to change background of button or your view in that method.
for more details:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphone101/Articles/05_ConfiguringView.html

Add a button to float on top of a ViewController, Path inspired

I am trying to build a button that will be floating on top of a ViewController, it should remain in the same position statically while the viewcontroller beneath it can scroll up and down.
I designed my own button class and in the viewdidload of the parent Viewcontroller I am doing this :
- (void)viewDidLoad// this is the parent viewcontroller
{
[super viewDidLoad];
customButton *floatingButton = [[UIButton alloc]initWithFrame:(self.view.frame)];
[floatingButton setLocation:CGPointMake(300, 430)];
[self.view addSubview:floatingButton];
[self.view bringSubviewToFront:floatingButton];
This is not doing it, as in when I run the button doesn't show up, only the view controller.
How can I achieve this?
I don't think that you're initializing your button properly. initWithFrame is a UIView initializer, being called by a regular UIButton. I don't see how you would get anything but an empty view here.
If you have a custom initializer for your custom button you should use
[[CustomButton alloc] myCustomInitializerWithFrame:frame];
Thanks guys, but the error was on my end, I wasn't initializing the frame properly, so the button was there but had a height and width of (0,0).

ios adding custom inputView

I'm stuck on a concept in iOS that I can't seem to understand, no matter how much I read about it. I'm trying to override the standard iOS number pad with a custom design. When the user touches the UITextField, I want the custom inputView to reveal instead of the standard number pad.
I created an separate .h/.m/.xib ViewController class for my custom inputView called "customInputViewController" Right now, it's just a dark background and one button that obscures about half of the screen when the UITextField is touched (similar to the number pad, but it just looks different). My implementation fails when I click the one button in my custom inputView -- iOS throws an EXC_BAD_ACCESS error.
This is how I load the .xib file at runtime and attach the custom inputView to the UITextField object:
UIViewController *v = [[customInputViewController alloc] initWithNibName:#"customInputDesign" bundle:nil];
myTextInput.inputView = v.view;
In the .xib file of the custom inputView, I set the File's Owner to be "customInputViewController" and I created an (IBAction) method and attached it to a UIButton. When that button is clicked, the (IBAction) is set up to send an NSLog(#"Button Clicked") message. Nothing special. It's just a simple boilerplate implementation that continues to throw an error.
Maybe I'm doing this entirely wrong. Can anyone provide a simple example?
The view v.view is retained as the inputView property is defined as (readwrite, retain). However, if you release your customInputViewController v somewhere before the input button is clicked, you will get a crash (EXC_BAD_ACCESS)
You can try this in your main controller:
- (IBAction) keyboardButtonClicked
{
NSLog(#"keyboard Button Clicked");
}
- (void) viewDidLoad
{
// do your stuff here ...
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; // add autorelease if you don't use ARC
v.backgroundColor = [UIColor darkGrayColor];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setTitle:#"Test button" forState:UIControlStateNormal];
[b addTarget:self action:#selector(keyboardButtonClicked) forControlEvents:UIControlEventTouchUpInside];
b.frame = CGRectMake(80, 25, 160, 50);
[v addSubview:b];
myTextInput.inputView = v;
}
Should work fine ...
First of all, Take a look at this
The UIKit framework includes support for custom input views and input
accessory views. Your application can substitute its own input view
for the system keyboard when users edit text or other forms of data in
a view. For example, an application could use a custom input view to
enter characters from a runic alphabet. You may also attach an input
accessory view to the system keyboard or to a custom input view; this
accessory view runs along the top of the main input view and can
contain, for example, controls that affect the text in some way or
labels that display some information about the text.
To get this feature if your application is using UITextView and
UITextField objects for text editing, simply assign custom views to
the inputView and inputAccessoryView properties. Those custom views
are shown when the text object becomes first responder...
Actually i don't need to mention all this mess to you, but there is an interesting reason for mentioning this, from the first sentence i am mentioning view-view-view, but you are making the input view in a separate view controller and you are trying to assign it as an input view of your textfield and init shouldn't be creating the view, loadView does that. Calling the view getter (v.view) when view is nil will cause loadView to be invoked.Thats why it is crashing with EXC_BAD_ACCESS.
Source : Text, Web, and Editing Programming Guide for iOS

How To Stop keyBoard Appearence at click event on UITextview?

In my IPad Application i am using TextView only for Text Displaying.As i need to display a Larger Text Thats Why i am using UITextview due to its Scrolling Property instead of using UILabel.
In my application i do not need to edit Text in UITextview ,but problem for me is that when i click on Textview for scrolling the keyboard appear its hide my textview so i want that my keyboard is never appear on click event.i make a search but not find any Suitable solution.Any help will be appriated.Thanx
NEW ANSWER (previous one was not working properly)
OK so since that is not working because it disables scrolling also, you should try to:
Implement UITextFieldDelegate protocol
In your view controller add the text
#interface YourViewController () <UITextViewDelegate>
In viewDidLoad set yourself as a delegate:
yourUITextView.delegate = self;
Implement the delegate method below:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return NO;
}
When the textview is about to edit the text, this method will be called automatically. It returns no, so the editing won't start.
It is very important that you undo the changes from the previous answers: Do not set the editable field to NO
I tried it and it's working. Hope it helps!
OLD ANSWER
when you declare the variable, or in your viewdidload method, set the editable property to NO:
yourUITextView.editable = NO;
or
[yourUITextView setEditable:NO]
That should prevent the keyboard from appearing.
Go to .XIB file and you can uncheck behavior editable or programmatically
textView.editable = NO;

Turn off highlighting on UIBarButtonItem

I'm trying to use a UIBarButtonItem to put a title on my UIToolbar. I'm using the plain style and that looks fine, but I can't seem to get it to stop highlighting on touch. The Shows Touch When Highlighted option isn't available for the bar button items. Is there a quick and easy way to do this? I'm trying to do the building in interface builder so I can see what I'm doing. I'd prefer not to build the toolbar in the view did load every time.
The property responsible for this is accessible in the UIButton class:
myButton.showsTouchWhenHighlighted = NO;
You can access this (programmatically) in a UIBarButtonItem by assigning a UIButton to the bar button item's customView property, and configuring the button. You can do this in Interface Builder too: drag a UIButton onto a UIToolbar, and it will automatically embed it in a UIBarButtonItem for you - then look for the "Shows Touch On Highlight" checkbox under the button's settings.
Incidentally, I don't know how you're customising your buttons so feel free to ignore this, but if your button looks and behaves like a standard toolbar item then users will expect the glow effect.
I wanted a solution that could be used without any modification to my XIB structure.
The most obvious and simple one worked: subclass UIBarButtonItem:
UITitleBarButtonItem.h:
//
// UITitleBarButtonItem.m
// Created by Guillaume Cerquant - MacMation on 09/08/12.
//
/*
* A UIBarButtonItem that does not show any highlight on the touch
* Drag and drop a normal UIBarButtonItem in your xib and set its subclass to UITitleBarButtonItem
*/
#interface UITitleBarButtonItem : UIBarButtonItem
#end
UITitleBarButtonItem.m:
#import "UITitleBarButtonItem.h"
#implementation UITitleBarButtonItem
// Only caring about UITitleBarButtonItem set up in Interface Builder. Update this class if you need to instantiate it from code
- (void) awakeFromNib {
UIView *theView = [self valueForKey:#"view"];
if ([theView respondsToSelector:#selector(setUserInteractionEnabled:)]) {
theView.userInteractionEnabled = NO;
}
}
#end
Tested on iOS 5 and the one we aren't allowed to talk yet.
Alternative: Use a UIBarButtonItem in the plain style and additionally cover the toolbar in the appropriate area with a UIView that has a clear background. The view consumes the taps and hides them from the bar button item. Make sure you set the autoresizing mask correctly.
My solution was to set it to disabled, and adjust the titleAttributes for each UIControlState
let attributes: [NSAttributedStringKey: Any] = [
.font: UIFont.boldSystemFont(ofSize: 16),
.foregroundColor: UIColor.white
]
barButton.setTitleTextAttributes(attributes, for: .enabled)
barButton.setTitleTextAttributes(attributes, for: .disabled)
barButton.isEnabled = false

Resources