我是iOS及其开发人员的新手。我已经阅读了Xcode旧样式在视图控制器之间传递数据。我已经看到很多事情都在使用xib文件。请在下面找到我用来传递数据的代码在ViewControllers之间。但是在 customelink (请找到DetailViewController.m)那里我得到Null值。请帮我在那里得到String值。并且足够解释什么是我在这里犯过的错误。
EssentialInfoController.h
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface Essentialinfocontroller : UIViewController
@end
EssentialInfoController.m
#import "Essentialinfocontroller.h"
@interface Essentialinfocontroller ()
@end
@implementation Essentialinfocontroller
- (void)viewDidLoad
{
[super viewDidLoad];
DetailViewController * customelink= [DetailViewController alloc ];
customelink.link=@"https://www.facebook.com";
}
@end
DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property(weak,nonatomic)NSString * link;
@end
DetailViewController.m
#import "DetailViewController.h"
@interface DetailViewController ()
@property(nonatomic,weak)NSString *customelink;
@end
@implementation DetailViewController
@synthesize link;
@synthesize customelink;
- (void)viewDidLoad
{
[super viewDidLoad];
self.customelink=self.link;
NSLog(@"link--> %@",customelink);// here 2014-07-06 21:21:51.469 WADTourisum[880:60b] link--> (null)
}
@end
答案 0 :(得分:1)
DetailViewController * customelink= [DetailViewController alloc ];
应该是
DetailViewController * customelink= [[DetailViewController alloc ] init];
和
@property(weak,nonatomic)NSString * link;
应该是
@property(strong,nonatomic)NSString * link;
并记录
NSLog(@"link--> %@",self.link);
你不需要这个
@property(nonatomic,weak)NSString *customelink;
如果您使用storyboard和segue,那么您必须实施
-(void)prepareForSegue
方法。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
假设您有button
从essentialVC
移至detailVC
,在您IBAction
中,请调用此方法:
[self performSegueWithIdentifier:@"YOUR_SEGUE_NAME_HERE" sender:self];
这只是一个简短的答案。我建议你首先阅读UINavigationController
,与segue,恕我直言相比,它更容易使用。