how to generate an event - iphone

I created a subclass from UIView
#import <UIKit/UIKit.h>
#interface MeeSelectDropDownView : UIView {
UILabel *mainText;
UIImage *bgImg;
UIImageView *bgView;
UIImageView *originView;
NSMutableArray *labelArray;
int selectedItem;
BOOL inSelectTag;
float _defaultHeight;
}
#property (nonatomic , retain) UIImage *bgImg;
#property (nonatomic , retain) UIImageView *bgView;
#property (nonatomic , retain) NSMutableArray *labelArray;
#property (nonatomic , retain) UIImageView *originView;
#property (nonatomic , retain) UILabel *mainText;
#property (nonatomic , readonly) int selectedItem;
- (void) setViewHeight:(float)aheight;
-(void) showDropList;
-(void) hiddenDropList;
-(void) setStringByArray:(NSArray*)array;
-(void)hiddenLabels
{
for(UILabel *aLabel in labelArray){
[aLabel removeFromSuperview];
}
}
Is it possible to generate an Event from function 'hiddenLabels' to inform and do somethings
Thanks
interdev

It is totally unclear what you are trying to do.
Suggestion: you can consider either posting an NSNotification, or registering observers using KVO (Key Value Observing).

Related

Display UIPickerView when UITextField is clicked

I have a screen with many text fields on it. For one particular text field, I need it to show a picker from which the user can pick state names, once the user has picked a state by clicking on the states name, the picker should disappear.
I am not quite sure how to do this, I have made the Nib file which looks like this.
And here is my header file:
#import <UIKit/UIKit.h>
#import "APIHelper.h"
#interface AddNewAddressViewController : UIViewController <UIPickerViewDelegate,
UIPickerViewDataSource, UITextFieldDelegate>
{
APIHelper *myAPIHelper;
NSMutableData *responseData;
NSURLConnection *theConnection;
UIPickerView *picker;
NSArray *stateNames;
#public IBOutlet UIImageView *imageNewAddressBox;
#public IBOutlet UIImageView *imageNewAddressBox1;
#public IBOutlet UIImageView *imageNewAddressBox2;
#public IBOutlet UIImageView *imageNewAddressBox3;
#public IBOutlet UILabel *labelNewAddress;
#public IBOutlet UILabel *labelStreetAddress;
#public IBOutlet UILabel *labelStreetAddressTwo;
#public IBOutlet UILabel *labelZipCode;
#public IBOutlet UILabel *labelState;
#public IBOutlet UILabel *labelCity;
#public IBOutlet UILabel *labelPhoneNumber;
#public IBOutlet UITextField *textFieldFullName;
#public IBOutlet UITextField *textFieldStreetAddress;
#public IBOutlet UITextField *textFieldPhoneNumber;
#public IBOutlet UITextField *textFieldStreetAddressTwo;
#public IBOutlet UITextField *textFieldCity;
#public IBOutlet UITextField *textFieldState;
#public IBOutlet UITextField *textFieldZipCode;
#public IBOutlet UIButton *buttonNext;
}
#property (nonatomic, strong) IBOutlet UIPickerView *picker;
#property (nonatomic, strong) NSArray *stateNames;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox1;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox2;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox3;
#property (nonatomic, strong) IBOutlet UILabel *labelNewAddress;
#property (nonatomic, strong) IBOutlet UILabel *labelStreetAddress;
#property (nonatomic, strong) IBOutlet UILabel *labelStreetAddressTwo;
#property (nonatomic, strong) IBOutlet UILabel *labelZipCode;
#property (nonatomic, strong) IBOutlet UILabel *labelState;
#property (nonatomic, strong) IBOutlet UILabel *labelCity;
#property (nonatomic, strong) IBOutlet UILabel *labelPhoneNumber;
#property (nonatomic, strong) IBOutlet UITextField *textFieldFullName;
#property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddress;
#property (nonatomic, strong) IBOutlet UITextField *textFieldPhoneNumber;
#property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddressTwo;
#property (nonatomic, strong) IBOutlet UITextField *textFieldCity;
#property (nonatomic, strong) IBOutlet UITextField *textFieldState;
#property (nonatomic, strong) IBOutlet UITextField *textFieldZipCode;
#property (nonatomic, strong) IBOutlet UIButton *buttonNext;
-(IBAction)addressTextFieldShouldReturn:(id)sender;
-(IBAction)nextPressed;
#end
And here is my .M file:
#import "AddNewAddressViewController.h"
#implementation AddNewAddressViewController
#synthesize imageNewAddressBox;
#synthesize imageNewAddressBox1;
#synthesize imageNewAddressBox2;
#synthesize imageNewAddressBox3;
#synthesize labelNewAddress;
#synthesize labelStreetAddress;
#synthesize labelStreetAddressTwo;
#synthesize labelZipCode;
#synthesize labelState;
#synthesize labelCity;
#synthesize labelPhoneNumber;
#synthesize textFieldFullName;
#synthesize textFieldStreetAddress;
#synthesize textFieldPhoneNumber;
#synthesize textFieldStreetAddressTwo;
#synthesize textFieldCity;
#synthesize textFieldState;
#synthesize textFieldZipCode;
#synthesize buttonNext;
#synthesize stateNames;
#synthesize picker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.stateNames = [[NSArray alloc] initWithObjects:#"Alabama", #"Alaska", #"Arizona", #"Arkansas", #"California", #"Colorado", #"Connecticut", #"Delaware", #"District of Columbia", #"Florida", #"Georgia", #"Guam", #"Hawaii", #"Idaho", #"Illinois", #"Indiana", #"Iowa", #"Kansas", #"Kentucky", #"Louisiana", #"Maine", #"Maryland", #"Massachusetts", #"Michigan", #"Minnesota", #"Mississippi", #"Missouri", #"Montana", #"Nebraska", #"Nevada", #"New Hampshire", #"New Jersey", #"New Mexico", #"New York", #"North Carolina", #"North Dakota", #"Ohio", #"Oklahoma", #"Oregon", #"Pennsylvania", #"Puerto Rico", #"Rhode Island", #"South Carolina", #"South Dakota", #"Tennessee", #"Texas", #"Utah", #"Vermont", #"Virginia", #"Virgin Islands", #"Washington", #"West Virginia", #"Wisconsin", #"Wyoming", nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// clicking outside the textfields removes keyboard
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(addressTextFieldShouldReturn:)];
[self.view addGestureRecognizer:tap];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setTitle: #"Update Address Book"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
-(IBAction)addressTextFieldShouldReturn:(id)sender
{
[textFieldFullName resignFirstResponder];
[textFieldStreetAddress resignFirstResponder];
[textFieldStreetAddressTwo resignFirstResponder];
[textFieldPhoneNumber resignFirstResponder];
[textFieldState resignFirstResponder];
[textFieldCity resignFirstResponder];
[textFieldZipCode resignFirstResponder];
[sender resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [stateNames count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
return [stateNames objectAtIndex:row];
}
#end
So, all I want it do is, when the user clicks on the textFieldState, a picker with all of the states name in it pops up from the bottom and then the user clicks on the states name and that name appears in textFieldState.
Any help would be greatly appreciated. Thanks.
you can use TextFielđelegate , is worked
#pragma mark- text delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField==txtMonth) {
[txtMonth resignFirstResponder];
if (monthClick==0) {
monthPicker.hidden=NO;
monthPicker = [[AFPickerView alloc] initWithFrame:CGRectMake(98.0, 123.0, 126.0, 197.0)];
monthPicker.dataSource = self;
monthPicker.delegate = self;
[monthPicker reloadData];
if ([txtMonth.text isEqualToString:#""]) {
txtMonth.text=[NSString stringWithFormat:#"%i",1];
}
[self.view addSubview:monthPicker];
monthClick=1;
}
}
}
I think you need to implement delegate methods for UITextField, when the
- (void)textFieldDidBeginEditing:(UITextField *)textField
method is called you set the frame of uipicker and show it.

Calling a function in viewcontroller from appdelegate

I want to call a function in a viewController from my appDelegate but with the following code, it doesn't get called. What am I doing wrong?
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "DetailsToTransfer.h"
#class AccountDetailTransferViewController;
#interface AccountDetailTransferAppDelegate : NSObject <UIApplicationDelegate> {
DetailsToTransfer *objDetailsToTransfer;
}
#property (nonatomic, retain) DetailsToTransfer *objDetailsToTransfer;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet AccountDetailTransferViewController *viewController;
-(void)sendToTransferScreen:(NSArray *)detailsArray;
#end
AppDelegate.m:
....
-(void)sendToTransferScreen:(NSArray *)detailsArray {
[objDetailsToTransfer setLabels:detailsArray];
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[self.window addSubview:objDetailsToTransfer.view];
}
DetailsToTransfer.h:
#import <UIKit/UIKit.h>
#interface DetailsToTransfer : UIViewController {
NSArray *accountDetailsArray;
UILabel *nameLabel;
UILabel *accountLabel;
IBOutlet UIButton *btnTransfer;
IBOutlet UIButton *btnBack;
}
#property (nonatomic, retain) NSArray *accountDetailsArray;
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UILabel *accountLabel;
-(IBAction)sendDetails;
-(IBAction)goBack;
-(void)setLabels:(NSArray *)array;
#end
DetailsToTransfer.m
....
-(void)setLabels:(NSArray *)array {
NSLog(#"Setting Labels");
self.nameLabel.text = [array objectAtIndex:0];
self.accountLabel.text = [array objectAtIndex:1];
}
....
I would like to add that all the properties have been synthesized and that I'm calling the method in the appDelegate properly (i checked using NSLogs)
In AppDelegate.m:
Looks as if you are calling a method on your object before the object has been created. Until you have alloc / init your object, there will be no labels to set text.
Try changing your method to this:
-(void)sendToTransferScreen:(NSArray *)detailsArray {
if (!objDetailsToTransfer) {
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[objDetailsToTransfer setLabels:detailsArray];
[self.window addSubview:objDetailsToTransfer.view];
}
}
The problem might be that you are trying to set the values on a object that hasn't been created yet. I also provided an if statement to prevent the object from being created multiple times.

Load a view .xib in MainWindow.xib

It`s probably a very silly mistake, but I've spent over 4 days looking for a solution for this.
It is very simple, I´ve got my MainView.xib and a view called FirstViewController (h/m/xib).
In MainWindow.xib I add a UIViewController and change the class name to FirstViewController and set the Nib name also (altouhg I've tried both ways).
I guess it has to do something with outlets, but I can`t really tell, as I am a newbie developing for iOS, any help wil REALLY help a lot.
Im using XCode 3.2 and interface builder, with SDK 4.3
AppDelegate
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface iPadTerritorioV2AppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet UIViewController *navigationController;
NSString *devToken;
NSString *matricula;
NSString *campus;
NSMutableArray *materiasAlumno; //para CCM
NSMutableArray *busqDir; //para CCM
NSInteger agendaBadgeNumber;
NSInteger intramurosBadgeNumber;
NSInteger notificacionesBadgeNumber;
NSInteger mapaBadgeNumber;
NSMutableData *receivedData;
NSMutableDictionary *listData;
BOOL yaSeHizoElPrimerFetchBadges;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIViewController *navigationController;
#property (nonatomic, retain) NSString *devToken;
#property (nonatomic, retain) NSString *matricula;
#property (nonatomic, retain) NSString *campus;
#property (nonatomic, retain) NSMutableArray *materiasAlumno;
#property (nonatomic, retain) NSMutableArray *busqDir;
#property NSInteger agendaBadgeNumber;
#property NSInteger intramurosBadgeNumber;
#property NSInteger notificacionesBadgeNumber;
#property NSInteger mapaBadgeNumber;
#property (nonatomic, retain) NSMutableData *receivedData;
#property (nonatomic, retain) NSMutableDictionary *listData;
#property BOOL yaSeHizoElPrimerFetchBadges;
- (void)fetchBadges;
#end
FirstViewController.h
#import <UIKit/UIKit.h>
#import "Constants.h"
#import "StringDecoding.h"
#define kConnectionBadgeNotifications 0
#define kConnectionLogin 1
#define kConnectionDevToken 2
#define kCCMindex 0
#define kCSFindex 1
#define kMTYindex 2
#interface FirstViewController : UIViewController {
IBOutlet UISegmentedControl *segmentedCampus;
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UISwitch *remembermeSwitch;
IBOutlet UIButton *loginButton;
UIActivityIndicatorView *loginIndicator;
NSMutableDictionary *listData;
NSMutableData *receivedData;
NSInteger connectionID;
}
#property (nonatomic, retain) UISegmentedControl *segmentedCampus;
#property (nonatomic, retain) UITextField *usernameField;
#property (nonatomic, retain) UITextField *passwordField;
#property (nonatomic, retain) UIActivityIndicatorView *loginIndicator;
#property (nonatomic, retain) UISwitch *remembermeSwitch;
#property (nonatomic, retain) UIButton *loginButton;
#property (nonatomic, retain) NSMutableDictionary *listData;
#property (nonatomic, retain) NSMutableData *receivedData;
#property NSInteger connectionID;
- (IBAction)handleNextClick:(id) sender;
- (IBAction)backgroundClick;
- (IBAction)login: (id) sender;
#end
It sounds like your FirstViewController isn't being retained - if it's not assigned to an outlet anywhere, nothing's retaining it and it'll just disappear. Add a property somewhere (your AppDelegate, perhaps, if that's all you've got) and connect it to the controller:
#property (nonatomic, retain) IBOutlet UIViewController *firstViewController;

Setting the value of a UITextField in an IBAction

I'm trying to use a UITextField as an output for a simple encryption. The field starts out with a nil string (its default behavior), and I try to update it in the encryptButtonPressed: action. I know I'm getting a good NSString into ciphertext, and I'm getting no errors or warnings or anything that the method doesn't exist, but the field remains blank. Any ideas?
Here's the header:
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *affineKeyField;
IBOutlet UITextField *shiftKeyField;
IBOutlet UITextField *messageField;
IBOutlet UITextField *ciphertextField;
MAAffineMachine* machine;
}
#property (nonatomic, retain) UITextField *affineKeyField;
#property (nonatomic, retain) UITextField *shiftKeyField;
#property (nonatomic, retain) UITextField *messageField;
#property (nonatomic, retain) UITextField *ciphertextField;
#property (nonatomic, retain) UIButton *encryptButton;
#property (nonatomic, retain) UIButton *clipboardButton;
- (IBAction)encryptButtonPressed:(id)sender;
- (IBAction)clipboardButtonPressed:(id)sender;
#end
And the action, defined in the controller's implementation:
- (IBAction)encryptButtonPressed:(id)sender {
NSLog(#"Encrypt Button pushed.\n");
int affine = [[affineKeyField text] intValue];
int shift = [[shiftKeyField text] intValue];
NSString* message = [messageField text];
NSLog(#"%d, %d, %#", affine, shift, message);
NSString* ciphertext = [machine encryptedStringFromString:message
ForAffine:affine
Shift:shift];
NSLog(#"%#\n", ciphertext);
[[self ciphertextField] setText: ciphertext];
}
If ciphertext has a proper value, you should check that ciphertextField is linked properly in the interface builder.
Try [[self ciphertextField] setText: ciphertext] -> [ciphertextField setText: ciphertext] or cipherTextField.text = ciphertext;

iPhone UIImageView Array

So I'm declaring a NSMutableArray to hold 5 UIImageViews.
.h file:
#interface ImageDisplay : UIViewController {
IBOutlet UIImageView *img1;
IBOutlet UIImageView *img2;
IBOutlet UIImageView *img3;
IBOutlet UIImageView *img4;
IBOutlet UIImageView *img5;
NSMutableArray *imageHolderArray;
}
#property (nonatomic, retain) IBOutlet UIImageView *img1;
#property (nonatomic, retain) IBOutlet UIImageView *img2;
#property (nonatomic, retain) IBOutlet UIImageView *img3;
#property (nonatomic, retain) IBOutlet UIImageView *img4;
#property (nonatomic, retain) IBOutlet UIImageView *img5;
#property (nonatomic, retain) IBOutlet NSMutableArray *imageHolderArray;
#end
In the .m file:
//All objects are synthesized, just easier not to crowd the screen
- (void)viewDidLoad {
[super viewDidLoad];
imageHolderArray = [[NSMutableArray alloc] initWithObjects: img1,img2,img3,img4,img5,nil];
NSLog(#"imageHolderArray count: %i",[imageHolderArray count]); //Returns count of 1
}
So my question is, why is this happening? Why isn't it picking up all the objects in the Array? I'm not well versed in Objective-C programming so I'd appreciate it if someone could clue me in here. Thank you.
Because you didn't wire the IBOutlets to their views in Interface Builder. Looks like you probably wired img1, but didn't wire img2, so img2 is nil, which marks the end of your list of objects for -initWithObjects: even if later outlets are wired.