我正在尝试传递String属性,我必须设置Label的文本。但是,字符串不会显示。我经历了多个视图控制器。想法是从textField获取信息,然后将其存储到属性中。然后将标签设置为以后的View控制器
中的字符串属性SentenceViewContoller.m
#import "SentenceViewController.h"
#import "DrawViewController.h"
@interface SentenceViewController ()
@end
@implementation SentenceViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.Sentence1TF.text = self.Sent1Str;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
- (IBAction)stopped:(id)sender {
}
@end
-----------------------------------------------------------------------
SentenceViewController.m
#import "SentenceViewController.h"
#import "DrawViewController.h"
@interface SentenceViewController ()
@end
@implementation SentenceViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.Sentence1TF.text = self.Sent1Str;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
- (IBAction)stopped:(id)sender {
}
@end
------------------------------------------------------------------------------
DrawViewController.h
#import <UIKit/UIKit.h>
@interface DrawViewController : UIViewController {
CGPoint lastPoint;
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat brush;
CGFloat opacity;
BOOL mouseSwiped;
}
@property (strong, nonatomic) IBOutlet UIImageView *tempDrawImage;
@property (strong, nonatomic) IBOutlet UIImageView *mainImage;
- (IBAction)pencilPressed:(id)sender;
- (IBAction)eraserPressed:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *Sentence1;
@property (weak, nonatomic) IBOutlet UIButton *Done;
@property (nonatomic, retain) NSString *Sent1Str;
@end
------------------------------------------------------------------------
DrawViewController.m
#import "DrawViewController.h"
#import "Draw2ViewController.h"
#import "SentenceViewController.h"
@interface DrawViewController ()
@end
@implementation DrawViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 10.0;
opacity = 1.0;
[super viewDidLoad];
//Do any additional setup after loading the view.
self.Sent1Str = ((SentenceViewController *)self.parentViewController).Sent1Str;
Draw2ViewController *D2V = [[Draw2ViewController alloc] init];
D2V.myImage = _mainImage;
self.Sentence1.text = self.Sent1Str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)pencilPressed:(id)sender {
UIButton * PressedButton = (UIButton*)sender;
switch(PressedButton.tag)
{
case 0:
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
break;
case 1:
red = 105.0/255.0;
green = 105.0/255.0;
blue = 105.0/255.0;
break;
case 2:
red = 255.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
break;
case 3:
red = 0.0/255.0;
green = 0.0/255.0;
blue = 255.0/255.0;
break;
case 4:
red = 102.0/255.0;
green = 204.0/255.0;
blue = 0.0/255.0;
break;
case 5:
red = 102.0/255.0;
green = 255.0/255.0;
blue = 0.0/255.0;
break;
case 6:
red = 51.0/255.0;
green = 204.0/255.0;
blue = 255.0/255.0;
break;
case 7:
red = 160.0/255.0;
green = 82.0/255.0;
blue = 45.0/255.0;
break;
case 8:
red = 255.0/255.0;
green = 102.0/255.0;
blue = 0.0/255.0;
break;
case 9:
red = 255.0/255.0;
green = 255.0/255.0;
blue = 0.0/255.0;
break;
}
}
- (IBAction)eraserPressed:(id)sender {
red = 255.0/255.0;
green = 255.0/255.0;
blue = 255.0/255.0;
opacity = 1.0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempDrawImage.image = nil;
UIGraphicsEndImageContext();
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
@end
-------------------------------------------------------------------------
Draw2ViewController.h
#import <UIKit/UIKit.h>
#import "DrawViewController.h"
#import "ResultsViewController.h"
@interface Draw2ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *Draw2Image;
@property (nonatomic, strong) UIImage *myImage;
@property (weak, nonatomic) IBOutlet UITextField *Sentence2TF;
- (IBAction)stopped2:(id)sender;
@property (nonatomic, retain) NSString *Sent1Str;
@property (nonatomic, retain) NSString *Sent2Str;
@end
------------------------------------------------------------------------
Draw2ViewController.m
#import "Draw2ViewController.h"
#import "DrawViewController.h"
#import "ResultsViewController.h"
@interface Draw2ViewController ()
@end
@implementation Draw2ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_Sentence2TF.text = _Sent2Str;
self.Sent1Str = ((DrawViewController *)self.parentViewController).Sent1Str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
- (IBAction)stopped2:(id)sender {
}
@end
---------------------------------------------------------------------------
ResultsViewController.h
#import <UIKit/UIKit.h>
@interface ResultsViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *ResultsImage;
@property (weak, nonatomic) IBOutlet UILabel *Sent1Results;
@property (weak, nonatomic) IBOutlet UILabel *Sent2Results;
@property (nonatomic, retain) NSString *Sent2Str;
@property (nonatomic, retain) NSString *Sent1Str;
@end
-----------------------------------------------------------------------------
ResultsViewController.m
#import "ResultsViewController.h"
#import "Draw2ViewController.h"
@interface ResultsViewController ()
@end
@implementation ResultsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.Sent1Str = ((Draw2ViewController *)self.parentViewController).Sent1Str;
self.Sent2Str = ((Draw2ViewController *)self.parentViewController).Sent2Str;
_Sent1Results.text = _Sent1Str;
_Sent2Results.text = _Sent2Str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end