【问题标题】:Call method from another view, I think?从另一个角度调用方法,我想?
【发布时间】:2013-02-16 20:50:31
【问题描述】:

我有一个简单的实用程序应用程序,带有 MainViewController.m & h 和 FlipsideViewController.m & h。在我的故事板中,我在 MainViewController 上有一个按钮。我希望能够单击按钮并在 FlipsideViewController.m 中运行一个方法,这可能吗?这是我的第一个应用程序,我完全是新手。欢迎所有 cmets / 建议。

enter code here

我的 FlipsideViewController.m 中有这个,这是我点击按钮时想要调用的内容。

- (void)SaveFPQData
{
NSLog(@"Data Saved");
}

这就是我在 MainViewController.m 中所拥有的

- (IBAction)saveButton:(id)sender
{

}

这是我目前的代码;

MainViewController.h

#import "FlipsideViewController.h"
#import "sqlite3.h"
#import "FPQCheck.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *checkField;
@property (weak, nonatomic) IBOutlet UITextField *commentsField;
@property (weak, nonatomic) FlipsideViewController *flipsidecontroller;

- (IBAction)saveButton:(id)sender;
- (IBAction)showHistoryButton:(id)sender;


@end

MainViewController.m

#import "MainViewController.h"
#import "FlipsideViewController.h"
#import "sqlite3.h"


@interface MainViewController ()

@end


@implementation MainViewController

- (void)viewDidLoad
{


[super viewDidLoad];
}

// Do any additional setup after loading the view, typically from a nib.


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showAlternate"]) {
    [[segue destinationViewController] setDelegate:self];
}
}


- (IBAction)saveButton:(id)sender
{
[self.flipsidecontroller SaveFPQData];


//[[NSNotificationCenter defaultCenter] postNotificationName:@"SaveFPQData" object:nil];
}

- (IBAction)showHistoryButton:(id)sender

{

}
@end

FlipSideViewController.h

#import <UIKit/UIKit.h>
#import "FPQCheck.h"

@class FlipsideViewController;

@protocol FlipsideViewControllerDelegate

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;


@end

@interface FlipsideViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>


@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;

-(void)SaveFPQData;


- (IBAction)done:(id)sender;
- (IBAction)deleteEntry:(id)sender;



@end

FlipSideViewController.m

#import "FlipsideViewController.h"
#import "MainViewController.h"


@interface FlipsideViewController ()
{
NSMutableArray *arrayOfCheck;
sqlite3 *fpqDB;
NSString *dbPathString;

}

@end

@implementation FlipsideViewController

- (void)viewDidLoad
{

/*
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(SaveFPQData)
                                             name:@"SaveFPQData"
                                           object:nil];
 */


[super viewDidLoad];
arrayOfCheck = [[NSMutableArray alloc]init];
[self creatOrOpenDB];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)SaveFPQData
{
NSLog(@"Data Saved");

}


-(void)creatOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"FPQ.db"];

char *error;

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
    const char *dbPath = [dbPathString UTF8String];

    //create db

    if (sqlite3_open(dbPath, &fpqDB)==SQLITE_OK) {
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS FPQ (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, CHECK INTEGER, COMMENTS TEXT)";
        sqlite3_exec(fpqDB, sql_stmt, NULL, NULL, &error);
        sqlite3_close(fpqDB);            
    }
}
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Actions

- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];}

- (IBAction)deleteEntry:(id)sender {

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfCheck count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell){
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

FPQCheck *fpqCheck = [arrayOfCheck objectAtIndex:indexPath.row];

NSString *nameANDcheck = [NSString stringWithFormat:@"%@%d", fpqCheck.name, fpqCheck.check];

cell.textLabel.text = nameANDcheck;
cell.detailTextLabel.text = fpqCheck.comments;

return cell;

}

@end

【问题讨论】:

  • 如何创建/显示 FlipsideViewController?

标签: objective-c xcode


【解决方案1】:

你主要有两种方式:

  1. 向您的MainViewController 添加一个属性(例如self.flipSideController)以存储对FlipsideViewController 的引用;然后通过它调用SaveFPQData(例如[self.flipSideController SaveFPQData];或

  2. 使用通知中心发布来自saveButton: 触发SaveFPQData 的通知;这将是这样的:

    //-- in flipsidecontroller `viewDidLoad`:
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(SaveFPQData)
                                             name:@"SaveFPQData"
                                           object:nil];
    
    //-- in saveButton:
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SaveFPQData" object:nil];
    

第二种方法实现起来最简单,IMO,它允许最松散的耦合,但会牺牲一些时钟周期。

编辑:

我并不完全清楚您要做什么(具体来说,我不完全了解在显示 FlipsideViewController 后如何在 MainViewController 中按下按钮;另一方面,如果您不继续FlipsideViewController,那么它不存在,所以你不能向它发送消息),无论如何你可以尝试在prepareForSegue 中初始化你的self.flipsideViewController 属性:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([[segue identifier] isEqualToString:@"showAlternate"]) {
    UIViewController* controller = [segue destinationViewController];
    [controller setDelegate:self];
    if ([controller isKindOfClass:[FlipsideViewController class]])
         self.flipsideViewController = (id)controller;
  }
}

完成此操作后,您的MainViewController 将能够将saveFPQ 消息发送到FlipsideViewController

如果您想将saveFPQ 消息之前 发送到FlipsideViewController,您应该将saveButton 转为它并调用saveFPQ 方法。

我怀疑您需要某种可从主视图和反面视图控制器访问的“模型”对象。

希望这会有所帮助。

【讨论】:

  • 为了清楚起见,我已经添加了到目前为止的代码,感谢您的帮助。
  • 请看我的编辑。如果没有帮助,请提供更多背景信息来说明您正在尝试做什么。
  • 嗨,塞尔吉奥,感谢您的帮助,我想我终于让它工作了,没有你我做不到。再次感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 2018-07-07
  • 2021-01-10
  • 2022-11-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多