그래, 다음 코드를 통해 하나의보기 컨트롤러에서 다른보기 컨트롤러로 데이터를 전달하려고 시도하지만 작동하지 않으며 그 이유를 모르겠습니다. 내 ViewController.m에서ViewController를 통한 데이터 전달
내가 ViewControllerTwo.h 수입 ViewControllerTwo 선언 : 내가 가지고있는 ViewControllerTwo.h에서
#import "ViewController.h"
#import "ViewControllerTwo.h"
@interface ViewController()
@end
@implementation ViewController
{
ViewControllerTwo *settings;
}
@synthesize blockSizeLB;
@synthesize wpmLB;
@synthesize textTV;
@synthesize slider;
@synthesize stepper;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//sets label to corresponding value
wpmLB.text = [NSString stringWithFormat:@"WPM: %@", [[NSNumber numberWithInt:slider.value] stringValue]];
//configure stepper and its label to default values of 1
stepper.value = 1;
blockSizeLB.text = [NSString stringWithFormat:@"Block Size: %@", [[NSNumber numberWithInt:stepper.value] stringValue]];
//sets properties for next ViewController
settings = [[ViewControllerTwo alloc] init];
settings.timerValue = 60/slider.value;
settings.text = textTV.text;
settings.blockCount = stepper.value;
}
:
@property (nonatomic) float timerValue;
@property (nonatomic) NSString * text;
@property (nonatomic) int blockCount;
을 나중에에 ViewController.m에서을 나는 필요 ViewControllerTwo에 정의 된 속성을 변경하십시오. ViewController.m의 메소드. 이것은 또한 속성의 기본 값을 설정하기 이전에 내있는 viewDidLoad에서 수행됩니다
- (IBAction)sliderSlide:(id)sender
{
//event when the slider value is changed
//rounds value to nearest increment of 5
int increment = 5 * floor(((int)slider.value/5)+0.5);
[slider setValue:increment animated:YES];
//changes WPM: 0 label text
wpmLB.text = [NSString stringWithFormat:@"WPM: %@", [[NSNumber numberWithInt:increment] stringValue]];
//sets properties for next ViewController
settings.timerValue = 60/slider.value;
}
나는이 NSLog를 통해 해당 속성 blockCount를 기록 ViewControllerTwo.m에서 메소드를 호출하여 성공하면 테스트하려고합니다. 그러나 결과는 ViewController.m에서 ViewController.m에서 데이터를 전달하는 데 성공하지 못했음을 의미합니다 (null).
을 어디에서 할 'settings' 변수를 실제'ViewControllerTwo' 객체를 가리 키도록 초기화 하시겠습니까? –
필자가 작성한 viewDidLoad 메서드를 포함하도록 업데이트했습니다. 그것은 ViewControllerTwo.m에서 합성되기 전에 Im 속성 값을 설정했기 때문일 수 있습니까? – user2692560
'settings'로 생성 한'ViewControllerTwo'의 인스턴스가 테스트중인 인스턴스와 같은지 의심 스럽습니다. ViewControllerTwo가'blockCount'를 출력 할 때 값을 지정할 때'settings'의 값을 기록하고'ViewControllerTwo'가'self'의 값을 기록하면 같은 주소를 표시합니까? –