我是一名新程序员,了解IOS和Objective C编程的基础知识,但遇到了一个错误。
我要做的就是点击一个按钮时,它会调用另一个类的方法。
我试图调用的方法是:[phoneCompany printPrompt];
所以这是我的代码:
头等舱:( ViewController)
的.m
#import "ViewController.h"
#import "PhoneCompany.h"
@implementation ViewController
@synthesize dialTextField;
@synthesize dialButton;
@synthesize textFromCall;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.dialTextField = [[UITextField alloc]initWithFrame:CGRectMake(83, 101, 154, 30)];
self.dialTextField.borderStyle = UITextBorderStyleRoundedRect;
self.dialTextField.placeholder = @"Dial Number";
self.dialTextField.textAlignment = NSTextAlignmentCenter;
self.dialTextField.adjustsFontSizeToFitWidth = YES;
self.dialTextField.minimumFontSize = 20;
self.dialTextField.autocorrectionType = NO;
self.dialTextField.returnKeyType = UIReturnKeyDone;
self.dialTextField.backgroundColor = [UIColor lightGrayColor];
self.dialTextField.delegate = self;
[self.view addSubview:self.dialTextField];
self.dialButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.dialButton setTitle:@"Dial!" forState:UIControlStateNormal];
self.dialButton.titleLabel.font = [UIFont systemFontOfSize:20];
[self.dialButton setBackgroundColor:[UIColor blueColor]];
[self.dialButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.dialButton addTarget:self action:@selector(mainCall) forControlEvents:UIControlEventTouchUpInside];
self.dialButton.frame =CGRectMake(92, 400, 125, 30);
[self.view addSubview:self.dialButton];
self.textFromCall = [[UILabel alloc]initWithFrame:CGRectMake(48,155,220,240)];
[self.textFromCall setText:@"Hello, what number would you like to call?"];
self.textFromCall.numberOfLines = 0;
self.textFromCall.lineBreakMode = UILineBreakModeWordWrap;
self.textFromCall.adjustsFontSizeToFitWidth = YES;
[self.textFromCall setTextAlignment:NSTextAlignmentCenter];
[self.textFromCall setTextColor:[UIColor blackColor]];
[self.textFromCall setBackgroundColor:[UIColor clearColor]];
[self.view addSubview: self.textFromCall];
}
-(void) mainCall{
if([self.dialTextField.text isEqualToString:@"1234567"]){
self.dialButton.enabled = NO;
self.dialTextField.enabled = NO;
PhoneCompany *phoneCompany = [[PhoneCompany alloc]init];
[NSTimer scheduledTimerWithTimeInterval: 3 target:phoneCompany selector:@selector(printPrompt)
userInfo:nil repeats:NO];
self.textFromCall.text = @"Dialing...";
[NSTimer scheduledTimerWithTimeInterval: 1 target:self selector:@selector(connectingStatement)
userInfo:nil repeats:NO];
}
else if([self.dialTextField.text isEqualToString: nil]){
self.textFromCall.text = @"Please enter a phone number.";
}
else{
self.textFromCall.text = @"Invalid Phone number.";
}
}
-(void)connectingStatement{
self.textFromCall.text = @"Connecting...";
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic) UITextField *dialTextField;
@property (weak,nonatomic) UIButton *dialButton;
@property (strong,nonatomic) UILabel *textFromCall;
-(void) mainCall;
-(void) connectingStatement;
-(void) setString:(NSString *)string;
@end
现在是第二类:( PhoneCompany)
·H
#import <Foundation/Foundation.h>
@interface PhoneCompany : NSObject
-(void) printPrompt;
@end
的.m
#import "PhoneCompany.h"
#import "ViewController.h"
@implementation PhoneCompany
-(void) printPrompt{
ViewController *mainView = [[ViewController alloc]init];
mainView.dialTextField.text = @"Test";
}
@end
答案 0 :(得分:0)
您对printPrompt的调用很好。问题是您正在函数中创建一个新的ViewContoller。您在printPrompt中创建的那个与您调用该函数的那个不同。我的意思是setString:不会替换textFromCall文本字段的文本。不知何故,您需要将ViewController作为委托传递给PhoneCompany,并从中调用setString:。
编辑:
试试这个 -
在PhoneCompany.h中
@class PhoneCompany;
@protocol PhoneCompanyDelegate <NSObject>
-(void)phoneCompany:(PhoneCompany *)phoneCompay
setString:(NSString *)string;
@end
@interface PhoneCompany : NSObject
@property (nonatomic, assign)id<PhoneCompanyDelegate>delegate;
- (id)initWithDelegate:(id<PhoneCompanyDelegate>)delegate;
- (void) printPrompt;
@end
在PhoneCompay.m
中@implementation PhoneCompany
- (id)initWithDelegate:(id<PhoneCompanyDelegate>)delegate
{
self = [super init];
if (self)
{
self.delegate = delegate;
}
return self;
}
-(void) printPrompt
{
if (self.delegate && [self.delegate respondsToSelector:@selector(phoneCompany:setString:)])
{
[self.delegate phoneCompany:self
setString:@"Test"];
}
}
@end
在prinPrompt
中创建PhonCompany对象时PhoneCompany *phoneCompay = [[PhoneCompany alloc] initWithDelegate:self];
在ViewController.h中
#import "PhoneCompany"
@interface ViewController:UIViewController<PhoneCompanyDelegate>
答案 1 :(得分:0)
事实证明,我所要做的就是将textFromCall声明为静态UILabel * textFromCall,然后声明一种编辑文本的方法。谢谢你的所有答案!