将文本字段的值传递给CocoaTouch上的函数

时间:2011-01-21 17:53:37

标签: iphone objective-c cocoa-touch

我在.h文件中有以下代码:

@interface ComposeViewController : UIViewController {
    id  delegate;
    IBOutlet UITextField *notificationTitle;
}
@property (nonatomic, assign) id  delegate;
- (IBAction)done:(id)sender;
- (void)scheduleAlert;
@end
并且在.m文件
- (void)scheduleAlert {
        // open an alert with just an OK button
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:(@"Hi") delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
[alert release]; }
上 我想将NSString *notificationTitle的内容放在*alert的消息支持上。如果我使用该代码,它会崩溃
- (void)scheduleAlert {
        // open an alert with just an OK button
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:(@"%@", notificationTitle)
                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
}
我如何将*notificationTitle传递给*alert消息?

提前谢谢你:)

2 个答案:

答案 0 :(得分:1)

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" 
                          message:notificationTitle
                          delegate:self 
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles: nil];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" 
                          message:[NSString stringWithFormat:@"This is the string: %@", notificationTitle]
                          delegate:self 
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles: nil];

答案 1 :(得分:0)

对于UIText字段,您应该使用其text属性,如此

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:notificationTitle.text delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];