I'm new at objective c, I was playing with labels buttons and textboxes, I've a label, text box and two buttons(Add, Clear),
what I want to do is, write something in the textbox and when press to "add" button I want the text to appear on the label , and the clear button should clear label and the text box
I've ran the program and the build is succeed however when i write some text and press return button nothing happens, the virtual keyboard still stays there and text doesn't appear on the label, my codes are as follows:
in the .h file
#interface ViewController : UIViewController
#property (retain, nonatomic) IBOutlet UILabel *label;
#property (retain, nonatomic) IBOutlet UITextField *txtbox;
#property (retain, nonatomic) IBOutlet UIButton *btnadd;
#property (retain, nonatomic) IBOutlet UIButton *btnclear;
- (IBAction)btnadd:(id)sender;
- (IBAction)btnclear:(id)sender;
- (IBAction)txtbox:(id)sender;
in the .m file
#implementation ViewController
#synthesize label,txtbox,btnadd,btnclear;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[label release];
[txtbox release];
[btnadd release];
[btnclear release];
[super dealloc];
}
- (IBAction)btnadd:(id)sender {
label.text = (txtbox.text);
}
- (IBAction)btnclear:(id)sender {
txtbox.text=#"";
label.text=#"";
}
- (IBAction)txtbox:(id)sender {
}
#end
I would really appreciate if someone helps, thanks in advance
P.s : what is the code to end the program if incase I add an exit button?
Thanks again,
Asim Gunduz
Have you linked everything correctly in your .xib/storyboard?
Read this on how to properly handle the return key.
You should not exit an app with a button. The only way to close your app should be pressing the home button.
However, you could use
exit(0);
the code looks fine, but you can try these tutorials,
Linking the IBOutlet and IBAction
Hiding Keyboard
Related
Before closing the post as duplicate,believe me,i am searching this for 2 days but still nothing.
I will try to be as clear as possible:
I have a view with a textview.I need to set focus automatically on the textview so the keyboard ill appear.
PostView.h
#interface PostView : UIViewController{
UITextView *txtPesto;
}
#property (nonatomic,retain) IBOutlet UITextView *txtPesto;
#end
PostView.m
#import "PostView.h"
#implementation PostView
#synthesize txtPesto;
- (void)viewDidLoad
{
[super viewDidLoad];
[txtPesto becomeFirstResponder];
}
For some very strange reason my code is not working,although i have tried many samples and different approaches.
Any help?
Answer from my comment (for closing question):
Right click on textView, and "new referncing outlet", maybe that will be helpful?
You are declaring txtPesto twice in your .h file:
#interface PostView : UIViewController{
UITextView *txtPesto;
}
#property (nonatomic,retain) IBOutlet UITextView *txtPesto;
#end
When you declare it as a property, you don't need to do it again. So remove the extra declaration, and just use this:
#interface PostView : UIViewController
#property (nonatomic,retain) IBOutlet UITextView *txtPesto;
#end
I don't see any other reason that becomeFirstResponder wouldn't work.
I'm new on this, and I would like to get some advice because I don't know what I'm doing wrong.
I want to make an app in xcode, with a UIView with some items, and when you do something, another UIView (smaller than the first) pops up above the first UIView. The popup UIView would be a customized class.
I have started with the UIViewController template and the initial UIView, and I have linked all the items in the .storyboard, and it works. But when I create my own UIView class (from objective-C class), put the second UIView over the first in the storyboard and link it to my class, something goes wrong.
The UIView appears, but when I try to set it to hidden, it doesn't answer. It's like it's not receiving the messages, so I think I don't link it well programmatically and just appears because of the storyboard.
I don't know if I have to create another UIViewController instead of the UIView, or if this is the correct path.
Can anybody explain me a little, or just write a little code snippet with the instantiation of the second view and adding it?
Lots of thanks!!
(I paste some code, of the declaration in .h and instantiation in .m)
#import <UIKit/UIKit.h>
#import "EditView.h"
#interface ReleaseViewController : UIViewController <UIWebViewDelegate, UISearchBarDelegate> {
IBOutlet UIWebView *web;
IBOutlet UISearchBar *search;
IBOutlet EditView *evHack;
}
#property (nonatomic, retain) IBOutlet UIWebView *web;
#property (nonatomic, retain) IBOutlet UISearchBar *search;
#property (nonatomic, retain) IBOutlet EditView *evHack;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
search.delegate = self;
web.delegate = self;
evHack = [evHack initWithFrame:CGRectMake(0, 44, 320, 377)];
[evHack setHidden:YES];
}
EditView Class (I still have nothing):
#import <UIKit/UIKit.h>
#interface EditView : UIView
#end
#import "EditView.h"
#implementation EditView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"View created");
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
initWithFrame only works when you alloc/init an app. If its already initialized, in this case by the storyboard, just set its frame:
evHack.frame = CGRectMake(0,44, 320, 377);
I don't know what it looks like in IB, But setting its frame in code may be redundant if you set it in IB too. To check whether evHack is hooked up right, NSLog evHack in viewDidLoad. If you get nil back, it's not hooked up right.
I would like the NavigationBar to behave the same but would like to change the appearance of it. I've found so many ways of doing this online but I'm not sure which one is the best result for iOS 5.0. The navigation bar will look like this:
Since you are targeting iOS 5 i would definitely go for customizing UINavigationBar using the Appearance proxy. Then you can easily set your own images and they will apply to all navigation bars in your application without subclassing.
You can also customize the buttons in the navigation bar by customizing UIBarButtonItem. There are method like backButtonBackgroundImageForState:barMetrics: for the back button and backgroundImageForState:barMetrics: for the other buttons.
I had been looking for this thing for ages, too, without finding a straightforward solution! Thanks to an friend of mine, and sample codes, we made it with a custom navigation bar class that can be imported into any view controller class.
The .h file:
#import <UIKit/UIKit.h>
#interface NATitleBar : UIView {
NSInteger tag;
}
#property ( nonatomic) IBOutlet UIImageView *imageView;
#property ( nonatomic) IBOutlet UILabel *label;
#property ( nonatomic) IBOutlet UIButton *back;
#property ( nonatomic) IBOutlet UIButton *home;
/**
* Supports UIButton-style adding targets
*/
#end
The .m file:
#import "NATitleBar.h"
#implementation NATitleBar
#synthesize imageView;
#synthesize label;
#synthesize back;
#synthesize home;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"NATitleBar" owner:self options:nil];
[self addSubview:[views objectAtIndex:0]];
// customize the view a bit
//self.imageView.layer.borderWidth = 1.0;
//self.imageView.layer.borderColor = [UIColor colorWithWhite:0.4 alpha:0.4].CGColor;
//self.imageView.clipsToBounds = YES;
//self.imageView.layer.cornerRadius = 5.0;
}
return self;
}
#pragma mark - Overriden Setters / Getters
- (void)setTag:(NSInteger)aTag {
self.back.tag = aTag;
}
- (NSInteger)tag {
return self.back.tag;
}
#end
and then for the Nib file we have the following:
You can add or delete images in the Nib file to make the GUI as you wish.
Now you must import the class into any view controller you wish to have with custom navigation controller, and also define two methods (or one, if you don't want the 'home' button. in .h :
- (void) back;
in .m:
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
I'm trying to change the text of a UILabel with text from an array upon a button click, but it doesn't do anything.
#interface Test01AppDelegate : NSObject <UIApplicationDelegate> {
UILabel *helloLabel;
UIButton *hellobutton;
NSMutableArray *madWords;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIButton *hellowButton;
#property (nonatomic, retain) IBOutlet UILabel *hellowLabel;
#property (nonatomic, retain) NSMutableArray *madWords;
- (void) madArrays;
- (IBAction)helloYall;
#end
and
#import "Test01AppDelegate.h"
#implementation Test01AppDelegate
#synthesize window = _window;
#synthesize hellowButton;
#synthesize hellowLabel;
#synthesize madWords;
- (void) madArrays {
[madWords addObject:#"Part A"];
[madWords addObject:#"Part B"];
[madWords addObject:#"Part C"];
[madWords addObject:#"Part D"];
}
- (IBAction)helloYall {
[self madArrays];
self.hellowLabel.text = [madWords objectAtIndex:0];
}
I can set the helloLabel text with
#"some text here";
and it works fine. Also, I tried copying the "madArrays" method into the "helloYall" method and it still didn't work. As I said, I can manually set the text and it works, but I'd like to pull the info from an array. Eventually, I'd like to loop through the array to grab the text on each button press, but one step at a time. Thanks.
You never create the madWords array. You need to add:
self.madWords = [NSMutableArray array];
at the top of:
- (void) madArrays {
would probably be a good place. Other possibly good places would be i the class init method or the view controller viewWillAppear method.
// Or you can try this in your init Method:
//first allocate the ivar
- (void)myInitMethod {
madArrays = [[NSMutableArray]alloc]init];
}
//then you can do anything directly to the ivar or throughout de setter
- (void)doAnythingWithiVar {
// do your stuff
}
//when you are done you can dealloc your ivar
- (void)dealloc {
[madArrays release];
[super dealloc];
}
It looks like madArrays is still nil when you come to populate it. At some point you need something like [self setMadArrays:[[NSMutableArray alloc] init]];. Also, don't forget to release madArrays in the dealloc before calling super as you'll have a memory leak.
Here is my code. Not sure what's awry.
controller.h
#import <UIKit/UIKit.h>
#interface ch4iOSPracticeViewController : UIViewController {
UITextField *nameField;
UITextField *numberField;
}
#property (nonatomic, retain) IBOutlet UITextField *nameField;
#property (nonatomic, retain) IBOutlet UITextField *numberField;
#end
controller.m
#import "ch4iOSPracticeViewController.h"
#implementation ch4iOSPracticeViewController
#synthesize nameField;
#synthesize numberField;
delegate.m Everything properly released.
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
When you add a textField to the iPhone using interface builder, I am guessing that there was another view already present there; So when you pulled the textField over it, it had stretched. Try pulling it inside a plain View.
It is either that you pulled a textview instead of a textfield.