ios - 将数据从登录 Controller 传递到 SWRevealViewController 的 frontviewcontroller

标签 ios iphone objective-c uistoryboardsegue swrevealviewcontroller

对不起我的英语。

我的问题是如何将数据从登录 View Controller 传递到 SWRevealViewController 库的 frontviewcontroller。让我解释一下我的情况。

我的应用有一个登录 View 。如果登录信息正常,您可以进入应用程序内部,菜单将由 SWRevealViewController 管理。

这是我的 Storyboard:

mystoryboard app

为了“记住”谁是已登录的用户,我想将一个 ID 从登录 View (在我的 Storyboard中称为“View Controller”)传递给“HomeappViewController”。

在我的 ViewController prepareforsegue 方法中,我编写了这段代码:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  if ([[segue identifier] isEqualToString:@"entrataApp"]) {
    SWRevealViewController *destination = [segue destinationViewController];
    UINavigationController *navViewController = (UINavigationController *) [destination frontViewController];
    HomeAppViewController *destViewController = (HomeAppViewController* )[navViewController topViewController];
    destViewController.testoPassato = risposta;
  }
}

在 HomeAppViewController 中,这是我的 viewDidLoad:

- (void)viewDidLoad{
   [super viewDidLoad];
   // Do any additional setup after loading the view.
   self.title = @"News";

   // Set the side bar button action. When it's tapped, it'll show up the sidebar.
   _sidebarButton.target = self.revealViewController;
   _sidebarButton.action = @selector(revealToggle:);

   // Set the gesture
   [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

   _datiPassati.text = _testoPassato;
}

如您所见,我尝试将包含我的“用户 ID”的“risposta”传递给 homeAppViewController 内的标签 destViewController.testoPassato,但是当我启动代码时,标签保持为空。

您认为我的错误是什么?也许我需要将 homeAppViewController 定义为 frontviewcontroller?我该怎么做?

如果我的问题很愚蠢,我很抱歉,但我是 iOS 应用程序开发的新手。

最佳答案

我想你需要知道谁在整个应用程序中登录用户你可以做的是在用户从 web 服务的响应登录时存储用户 id

您可以使用以下方法存储它

[[NSUserDefaults standardUserDefaults] setValue:[dictData valueForKey:@"id"] forKey:@"uid"];

这里的dictData是我的响应字典,field是id

进一步将这些添加到您的常量或.pch 文件中

#define UserId     [[NSUserDefaults standardUserDefaults] valueForKey:@"uid"]

然后在应用程序中任何类的任何 viewController 中的任何需要的地方使用 UserId。

当你想在应用程序中提供注销功能时给

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

这些将删除所有 key ,当您执行应用程序时,它将再次进入登录页面。

关于ios - 将数据从登录 Controller 传递到 SWRevealViewController 的 frontviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23214491/

相关文章:

iphone - iOS GM GridView 如何重用GM GridView Cell 单元格?

ios - 向sqlite数据库中插入数据

iphone - 从 python 向 iphone 发送推送通知

iphone - iOS : Is there a way to kill current view controller of NavigationControl before navigating to other view controller?

iphone - 为什么addSubview是: not retaining the view?

objective-c - 如何全局更改UIKit属性的默认值?

ios - 如何检测字典是否为空或为 null

ios - 无法启动应用程序(验证开发者应用程序证书)

iphone - 通过 MKPolyline 获取中间点

ios - 如何防止Mobile Safari 炸毁字体?