hafedh
hafedh

Reputation: 539

How to make a class as a delegate for another?

I am learning objective-c and I get some trouble with delegates.

In a test application, i have a viewcontroller having a IUbutton, and another UITableViewcontroller. I want to make the TableViewController appears when I click the button.

the problem that the window object is not identified in this ViewController.

[self.window addSubview:viewController.view]; this line shows errors.

I think I should delegate the appdelagate to my viewcontroller ? how to do ?

Upvotes: 1

Views: 641

Answers (4)

Seyther
Seyther

Reputation: 662

i would guide you through this in a tutorial.
1) Create a new project in xcode using view application template
2) Open up your view's xib file
3) Drag a UITableView and a UIButton from the library to the view.
4) Select your UITableView view and checked the hidden property.
5) Go to your view's header file and declare two IBOutlets (for your button and table), 1 IBAction (for your button's target action).
6) Link up the outlets to the respective UI objects in your Interface Builder and set the target action of the button (touch up event) to your controller.
7) In your IBAction, set the hidden property of the UITableView to NO and button's hidden property to YES.
8) Volia, you should see your table view now when you click on the button and the button would become hidden after you clicked

I skip the part on adding the tableview delegates so you will not see anything in your table. You can check them out on Apple's UITableView example.

EDIT: In your IBAction for the button, put something similar like this
- Require a navigation controller: [self.navigationController pushViewController:putTheNameOfYourTableViewControllerHere animated:YES];
- Present in a modal view:
[self presentModalViewController:putTheNameOfYourTableViewControllerHere animated:YES];

Upvotes: 0

Saurabh
Saurabh

Reputation: 22893

I just copy pasted this code form my project.. I have created a delegate in this class "appImageDidLoad" this delegate called when the image downloaded completely. Hope it will help you can post comment if you need any explaination

#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

Upvotes: 0

Ishu
Ishu

Reputation: 12787

you cant access the window by self you need an object of appDelegate class

so code like this

yourAppDelegate *objDelegate=(yourAppDelegate *)[[UIApplication sharedApplication] delegate];

now you can access the window,

[objDelegate.window addSubview:viewController.view];

Upvotes: 0

visakh7
visakh7

Reputation: 26400

Add this in the applicationDidFinishLaunching method of app delegate

[self.window addSubview:viewController.view];

Or if you want to access the main window in a view controller u can refer this link

Referencing AppDelegate instance variables

Upvotes: 1

Related Questions