可以通过单击其他视图控制器内的按钮来更改图像吗?我使用此代码将数据从 ViewController 传递到 SecondViewController。我想将突击队添加到同一个按钮以将图像添加到 SecondViewController ......请原谅我的糟糕问题......
视图控制器.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController <SecondViewControllerDelegate> {
IBOutlet UITextField *userNameTextField;
}
@property (nonatomic, strong) UITextField *userNameTextField;
@end
视图控制器.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"to2"]){
SecondViewController *viewController = segue.destinationViewController;
viewController.delegate = self;
}
}
- (void)done:(NSString *)name{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Back in first ViewController, metod Done, with name=%@",name);
userNameTextField.text=name;
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>
-(void) done:(NSString*)someText;
@end
@interface SecondViewController : UIViewController {
IBOutlet UITextField *someText;
IBOutlet UIButton *returnButton;
id delegate;
}
@property (nonatomic, assign) id <SecondViewControllerDelegate> delegate;
@property (nonatomic, strong) UITextField *someText;
- (IBAction)returnButtonPressed:(id)sender;
@end
第二视图控制器.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize someText;
@synthesize delegate = _delegate;
- (IBAction)returnButtonPressed:(id)sender {
[self.delegate done:someText.text];
}
@end