Sha*_*awn 3 xcode class objective-c ios
我正在尝试做一个ios应用程序,但我坚持在类之间传递数据.这是我的第二个应用程序.第一个是在全局类中完成的,但现在我需要多个类.我尝试了很多教程,但是没有用,或者传递的值总是为零.有人可以给我写一个简单的应用程序,以证明在IOS 5中传递变量.没什么特别的,故事板连两个视图控制器,一个变量.
谢谢您的帮助 .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
FirstViewController *fv;
fv.value = indexPath.row;
NSLog(@"The current %d", fv.value);
FirstViewController *detail =[self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
这是我的主视图中的代码,我需要发送indexPath.row或我按下的单元格的索引到下一个视图
Mar*_*1ni 12
有几件事要做.根据应用程序的不同,您可以向AppDelegate类添加一个变量,使其通过共享实例可用于所有类.最常见的事情(我认为)是制作一个单身人士.为了实现这一点,您可以创建一个类,比如说StoreVars,以及一个返回该对象的静态方法,这使得该类成为"全局".在该方法中,您可以像往常一样初始化所有变量.然后你总是可以从任何地方到达他们.
@interface StoreVars : NSObject
@property (nonatomic) NSArray * mySharedArray;
+ (StoreVars*) sharedInstance;
@implementation StoreVars
@synthesize mySharedArray;
+ (StoreVars*) sharedInstance {
static StoreVars *myInstance = nil;
if (myInstance == nil) {
myInstance = [[[self class] alloc] init];
myInstance.mySharedArray = [NSArray arrayWithObject:@"Test"];
}
return myInstance;
}
Run Code Online (Sandbox Code Playgroud)
这将成为一个单身人士.如果你记得在两个viewControllers中导入"StoreVars.h",你可以像这样访问现在共享的数组;
[StoreVars sharedInstance].mySharedArray;
^
Run Code Online (Sandbox Code Playgroud)
这是一个返回StoreVars对象的方法.在StoreVars类中,您可以实现任何对象并在静态方法中对其进行初始化.只记得要初始化它,否则,你的所有对象都是0/nil.
如果你不是UINavigationController的粉丝而宁愿使用segues,它会更容易,但可以使你的应用程序相当"混乱"imo.在UIViewController中实现的方法应该重载:
- (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];
}
}
Run Code Online (Sandbox Code Playgroud)
source:如何传递prepareForSegue:一个对象
在提出这样的问题之前做一些研究.阅读一些教程,然后自己尝试,然后提出与您真正想要的相关的问题.并不是每天都有人想为你做所有的工作,但有时候你很幸运.好像今天.
干杯.
| 归档时间: |
|
| 查看次数: |
22720 次 |
| 最近记录: |