我正在学习目标-c而且我对代表们有些麻烦。
在测试应用程序中,我有一个具有IU按钮的viewcontroller和另一个UITableViewcontroller。我想在单击按钮时显示TableViewController。
在此ViewController中未识别窗口对象的问题。
[self.window addSubview:viewController.view];
此行显示错误。
我想我应该将appdelagate委托给我的viewcontroller?怎么办?
答案 0 :(得分:1)
在app delegate的applicationDidFinishLaunching
方法中添加
[self.window addSubview:viewController.view];
或者,如果您想访问视图控制器中的主窗口,可以参考此链接
答案 1 :(得分:0)
你不能自己访问窗口,你需要一个appDelegate类的对象
所以像这样的代码
yourAppDelegate *objDelegate=(yourAppDelegate *)[[UIApplication sharedApplication] delegate];
现在您可以访问该窗口,
[objDelegate.window addSubview:viewController.view];
答案 2 :(得分:0)
我只是复制粘贴这个代码形式我的项目..我在这个类中创建了一个委托“appImageDidLoad”这个委托在完全下载图像时调用。如果您需要任何解释,希望它可以帮助您发表评论
#import "MixtapeInfo.h"
@class Record;
@protocol IconDownloaderDelegate;
@interface IconDownloader : NSObject
{
MixtapeInfo *appRecord;
NSIndexPath *indexPathInTableView;
id <IconDownloaderDelegate> delegate;
NSMutableData *activeDownload;
NSURLConnection *imageConnection;
}
@property (nonatomic, retain) MixtapeInfo *appRecord;
@property (nonatomic, retain) NSIndexPath *indexPathInTableView;
@property (nonatomic, assign) id <IconDownloaderDelegate> delegate;
@property (nonatomic, retain) NSMutableData *activeDownload;
@property (nonatomic, retain) NSURLConnection *imageConnection;
- (void)startDownload;
- (void)cancelDownload;
@end
@protocol IconDownloaderDelegate
- (void)appImageDidLoad:(NSIndexPath *)indexPath;
@end
#import "IconDownloader.h"
#import "MixtapeInfo.h"
#define kAppIconHeight 48
#define TMP NSTemporaryDirectory()
@implementation IconDownloader
@synthesize appRecord;
@synthesize indexPathInTableView;
@synthesize delegate;
@synthesize activeDownload;
@synthesize imageConnection;
#pragma mark
- (void)dealloc
{
[appRecord release];
[indexPathInTableView release];
[activeDownload release];
[imageConnection cancel];
[imageConnection release];
[super dealloc];
}
- (void)startDownload
{
self.activeDownload = [NSMutableData data];
// alloc+init and start an NSURLConnection; release on completion/failure
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:appRecord.mixtape_image]] delegate:self];
self.imageConnection = conn;
[conn release];
}
- (void)cancelDownload
{
[self.imageConnection cancel];
self.imageConnection = nil;
self.activeDownload = nil;
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.activeDownload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Clear the activeDownload property to allow later attempts
self.activeDownload = nil;
// Release the connection now that it's finished
self.imageConnection = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Set appIcon and clear temporary data/image
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
self.appRecord.mixtape_image_obj = image;
self.activeDownload = nil;
[image release];
// Release the connection now that it's finished
self.imageConnection = nil;
// call our delegate and tell it that our icon is ready for display
[delegate appImageDidLoad:self.indexPathInTableView];
}
@end
答案 3 :(得分:0)
我将在教程中指导您完成此任务
1)使用视图应用程序模板在xcode中创建一个新项目
2)打开视图的xib文件
3)将UITableView和UIButton从库拖到视图中
4)选择您的UITableView视图并检查隐藏属性
5)转到视图的头文件并声明两个IBOutlets(用于按钮和表格),1 IBAction(用于按钮的目标操作)。
6)将出口链接到Interface Builder中的相应UI对象,并将按钮的目标操作(触摸事件)设置到控制器。
7)在您的IBAction中,将UITableView的隐藏属性设置为NO,将按钮的隐藏属性设置为YES。
8)Volia,当你点击按钮时你应该看到你的桌面视图,点击后按钮会被隐藏
我跳过了添加tableview委托的部分,因此您不会在表中看到任何内容。您可以在Apple的UITableView示例中查看它们。
修改强>
在你的按钮的IBAction中,放置类似这样的东西
- 需要导航控制器:
[self.navigationController pushViewController:putTheNameOfYourTableViewControllerHere animated:YES];
- 以模态视图出现:
[self presentModalViewController:putTheNameOfYourTableViewControllerHere animated:YES];