Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标c适当授权_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Objective c 目标c适当授权

Objective c 目标c适当授权,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我对objective-c还不熟悉,也许我还没有很清楚地理解授权的概念,但我希望通过使用它来实现。我正在尝试在我的应用程序中实现委派 我的想法是让类TableViewController具有NSMutableArray用于TableView初始化。我需要从我的下拉列表中重新初始化这个数组。我试着用委托的方式去做,但还是失败了,也许是有什么问题。我可以将TableViewController传递到DropDown类,并通过对象编辑表。但我想通过授权来完成 这是我的TableViewControll

我对objective-c还不熟悉,也许我还没有很清楚地理解授权的概念,但我希望通过使用它来实现。我正在尝试在我的应用程序中实现委派

我的想法是让类
TableViewController
具有
NSMutableArray
用于
TableView
初始化。我需要从我的
下拉列表中重新初始化这个数组。我试着用委托的方式去做,但还是失败了,也许是有什么问题。我可以将
TableViewController
传递到
DropDown
类,并通过对象编辑表。但我想通过授权来完成

这是我的
TableViewController.h

@protocol TableViewControllerdelegate;
@interface TableViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,MFMessageComposeViewControllerDelegate>
{
    ControllerType controllerType;
}

@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (retain, nonatomic) NSMutableArray *dataArray;
@property (retain, nonatomic) NSArray *imageArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andType:(ControllerType)type;
- (void)sendSMS: (NSString *) sms;

@end;
这就是我试图编辑tableview对象数组的方式

            TableViewController *newControll = (TableViewController*)[UIApplication sharedApplication].delegate;
            NSMutableArray *arrayWithInfo = [[NSMutableArray alloc] initWithObjects:AMLocalizedString(@"Status", nil),AMLocalizedString(@"Call", nil),AMLocalizedString(@"Location", nil),AMLocalizedString(@"Control", nil),AMLocalizedString(@"Sim", nil),AMLocalizedString(@"Object", nil),AMLocalizedString(@"Info", nil),nil];
            newControll.dataArray = arrayWithInfo;
            [arrayWithInfo release];
            [newControll.tableView reloadData];

我让它运行,但它是get的
”-[AppDelegate setDataArray::]:到达此代码后,无法识别的选择器被发送到实例。

您会收到该错误,因为(正如错误所说)您正在向您的应用程序委托(AppDelegate类)发送
setDataArray:
消息

这将返回应用程序的代理。有两种方法可以确定哪个类是应用程序的委托,但通常称为AppDelegate(如您的情况),并且它也在实现
UIApplicationDelegate
协议

你不能简单地把它转换成一个完全不同的类。如果您的应用程序代理具有类型为
TableViewController
的ivar或属性,则必须使用访问器来获取它。如果它是一个属性,可以使用点表示法。如果是ivar,则可以实现返回ivar的getter方法,或者将其作为属性

// assuming your app delegate has a TableViewController property called myTableViewController.
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
TableViewController *tableViewController = appDelegate.myTableViewController;

这将修复错误,但是您使用委托模式也是错误的。我看不到您在哪里使用任何自定义代理。您正向声明了一个
TableViewControllerdelegate
协议,但我没有看到它的任何声明,或者我没有看到您试图在哪里使用它。

好的,我不确定我是否正确,但它最终为我单击了什么是委托以及我为什么需要它。希望你看完我的剧本后也能理解

历史 以前,在我的UITabBar应用程序中,我想显示一个覆盖在视图控制器顶部的自定义表单视图,以输入名称和电子邮件

稍后,我还需要在另一个选项卡上的另一个视图控制器顶部显示相同的自定义覆盖

当时我还不知道委派是为了什么,所以我用来解决这个问题的第一个方法是NSNotificationCenter。我将相同的代码复制到第二个视图控制器,并将其连接到按钮按下事件

按下另一个选项卡上第二个视图控制器上的按钮时,它肯定会显示我的自定义覆盖,就像我的第一个视图控制器一样

然而,问题就从这里开始

问题 我需要关闭自定义表单视图。因此,使用NSNotificationCenter,我发布了一个通知,通知的侦听器回调方法被告知关闭我的自定义视图

问题是,使用NSNotificationCenter,我的第一个选项卡和第二个选项卡中的所有侦听器都对发布的通知作出响应,因此,它没有关闭覆盖在第二个视图控制器上的自定义表单视图,而是关闭了我的所有自定义视图,无论自定义视图从何处打开

我想要的是,当我点击“X”按钮关闭我的自定义表单视图时,我只希望它关闭自定义视图的单个实例,而不是我打开的所有其他实例

解决办法:授权 这就是我最后点击的地方——授权

通过委托,我会告诉自定义表单视图的每个实例谁是委托人,如果我点击“X”按钮关闭自定义视图,它只会关闭打开的单个实例,其他所有视图控制器都不会被触动

一些代码 对,就是一些代码

不确定这是否是最好的方法(如果我错了,请纠正我),但我就是这样做的:

// ------------------------------------------------------------
// Custom Form class .h file
// ------------------------------------------------------------

@protocol MyCustomFormDelegate <NSObject>

// if you don't put a @optional before any method, then they become required
// in other words, you must implement these methods
-(void)sendButtonPressed;
-(void)closeButtonPressed;

// example: these two methods here does not need to be implemented
@optional
-(void)optionalMethod1;
-(void)optioinalMethod2;

@end

@interface MyCustomFormView : UIView
{
    ...
    id<MyCustomFormDelegate> delegate;
}

...
@property (nonatomic, retain) id<MyCustomFormDelegate> delegate;

@end


// ------------------------------------------------------------
// Custom Form class .m file
// ------------------------------------------------------------

...

@implementation TruckPickerView

@synthesize delegate;

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if(self)
    {
        ...

        [btnSend addTarget:self selector:@selector(sendEmail) forControlEvent:UIControlEventTouchUpInside];

        ...

        [btnClose addTarget:self selector:@selector(closeForm) forControlEvent:UIControlEventTouchUpInside];
    }

    return self;
}

-(void)sendEmail
{
    // code sends email

    ...

    // ------------------------------------------------------------
    // tell the delegate to execute the delegate callback method
    //
    // note: the implementation will be defined in the 
    // view controller (see below)
    // ------------------------------------------------------------
    [delegate sendButtonPressed];
}

-(void)closeForm
{
    // ------------------------------------------------------------
    // tell the delegate to execute the delgate callback method
    //
    // note: the implementation will be defined in the 
    // view controller (see below)
    // ------------------------------------------------------------
    [delegate closeButtonPressed];
}



// ------------------------------------------------------------
// view controller .h file
// ------------------------------------------------------------

#import "MyCustomFormView.h"

// conform to our delegate protocol
@interface MyViewController <MyCustomFormDelegate>
{
    ...

    // create a single instance of our custom view
    MyCustomFormView *customForm;
}

@property (nonatomic, retain) MyCustomFormView *customForm;


// ------------------------------------------------------------
// view controller .m file
// ------------------------------------------------------------

@synthesize customForm;

-(void)viewDidLoad
{
    customForm = [[MyCustomFormView alloc] initWithFrame:....];

    // tell our custom form this view controller is the delegate
    customForm.delegate = self;

    // only show the custom form when user tap on the designated button
    customForm.hidden = YES;

    [self.view addSubview:customForm];
}

-(void)dealloc
{
    ...
    [customForm release];
    [super dealloc];
}

// helper method to show and hide the custom form
-(void)showForm
{
    customForm.hidden = NO;
}

-(void)hideForm
{
    customForm.hidden = YES;
}

// ------------------------------------------------------------
// implement the two defined required delegate methods
// ------------------------------------------------------------
-(void)sendButtonPressed
{
    ...

    // email has been sent, do something then close 
    // the custom form view afterwards

    ...

    [self hideForm];
}

-(void)closeButtonPressed
{
    // Don't send email, just close the custom form view 
    [self hideForm];
}
//------------------------------------------------------------
//自定义表单class.h文件
// ------------------------------------------------------------
@协议MyCustomFormDelegate
//如果您没有在任何方法之前添加@optional,那么它们就成为必需的
//换句话说,您必须实现这些方法
-(无效)按下发送按钮;
-(无效)按下关闭按钮;
//示例:这里不需要实现这两种方法
@可选的
-(无效)选择方法1;
-(无效)选择方法2;
@结束
@接口MyCustomFormView:UIView
{
...
id代表;
}
...
@属性(非原子,保留)id委托;
@结束
// ------------------------------------------------------------
//自定义表单类.m文件
// ------------------------------------------------------------
...
@实现TruckPickerView
@综合代表;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我)
{
...
[BTN发送添加目标:自选择器:@selector(发送电子邮件)forControlEvent:UIControlEventTouchUpInside];
...
[btnClose addTarget:self selector:@selector(closeForm)for ControlEvent:UIControlEventTouchUpInside];
}
回归自我;
}
-(作废)发送电子邮件
{
//代码发送电子邮件
...
// ------------------------------------------------------------
//告诉委托人执行委托回调方法
//
//注:实施将在
//视图控制器(见下文)
// ------------------------------------------------------------
[按下代表发送按钮];
}
-(无效)封闭式表格
{
// ------------------------------------------------------------
//告诉代理执行delgate回调方法
//
//注:实施将在
//视图控制器(见下文)
// ---------------------------------------------------------
// assuming your app delegate has a TableViewController property called myTableViewController.
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
TableViewController *tableViewController = appDelegate.myTableViewController;
// ------------------------------------------------------------
// Custom Form class .h file
// ------------------------------------------------------------

@protocol MyCustomFormDelegate <NSObject>

// if you don't put a @optional before any method, then they become required
// in other words, you must implement these methods
-(void)sendButtonPressed;
-(void)closeButtonPressed;

// example: these two methods here does not need to be implemented
@optional
-(void)optionalMethod1;
-(void)optioinalMethod2;

@end

@interface MyCustomFormView : UIView
{
    ...
    id<MyCustomFormDelegate> delegate;
}

...
@property (nonatomic, retain) id<MyCustomFormDelegate> delegate;

@end


// ------------------------------------------------------------
// Custom Form class .m file
// ------------------------------------------------------------

...

@implementation TruckPickerView

@synthesize delegate;

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if(self)
    {
        ...

        [btnSend addTarget:self selector:@selector(sendEmail) forControlEvent:UIControlEventTouchUpInside];

        ...

        [btnClose addTarget:self selector:@selector(closeForm) forControlEvent:UIControlEventTouchUpInside];
    }

    return self;
}

-(void)sendEmail
{
    // code sends email

    ...

    // ------------------------------------------------------------
    // tell the delegate to execute the delegate callback method
    //
    // note: the implementation will be defined in the 
    // view controller (see below)
    // ------------------------------------------------------------
    [delegate sendButtonPressed];
}

-(void)closeForm
{
    // ------------------------------------------------------------
    // tell the delegate to execute the delgate callback method
    //
    // note: the implementation will be defined in the 
    // view controller (see below)
    // ------------------------------------------------------------
    [delegate closeButtonPressed];
}



// ------------------------------------------------------------
// view controller .h file
// ------------------------------------------------------------

#import "MyCustomFormView.h"

// conform to our delegate protocol
@interface MyViewController <MyCustomFormDelegate>
{
    ...

    // create a single instance of our custom view
    MyCustomFormView *customForm;
}

@property (nonatomic, retain) MyCustomFormView *customForm;


// ------------------------------------------------------------
// view controller .m file
// ------------------------------------------------------------

@synthesize customForm;

-(void)viewDidLoad
{
    customForm = [[MyCustomFormView alloc] initWithFrame:....];

    // tell our custom form this view controller is the delegate
    customForm.delegate = self;

    // only show the custom form when user tap on the designated button
    customForm.hidden = YES;

    [self.view addSubview:customForm];
}

-(void)dealloc
{
    ...
    [customForm release];
    [super dealloc];
}

// helper method to show and hide the custom form
-(void)showForm
{
    customForm.hidden = NO;
}

-(void)hideForm
{
    customForm.hidden = YES;
}

// ------------------------------------------------------------
// implement the two defined required delegate methods
// ------------------------------------------------------------
-(void)sendButtonPressed
{
    ...

    // email has been sent, do something then close 
    // the custom form view afterwards

    ...

    [self hideForm];
}

-(void)closeButtonPressed
{
    // Don't send email, just close the custom form view 
    [self hideForm];
}