4

I am making a download queueing system to download videos. The queueing code to handle the downloads is in another viewconntroller. Now my question is how can i pass the URL of the download to the other view without pushing to another view controller like this:

 ViewConntroller *View = [[ViewConntroller alloc] init];
    view.url = ""
   [self presentModalViewController:View animated:NO];

I also know there is a way through delegates but don't you also have to weight for the view to be called upon aswell?

EDIT I have an NSURL in which i want to send to another viewconntroller. I know there is the method of Protocols or the method above. However i do not want to present a viewconntroller to send the info. I just want to send it as soon as the button is pressed without going to the view itself

Tom
  • 640
  • 1
  • 7
  • 25

3 Answers3

2

I do not want to present a viewconntroller to send the info. I just want to send it as soon as the button is pressed without going to the view itself

Generally, you cannot do that: you could send the data back the chain of view controllers, but sending it forward is possible only when you present the view controller to which you send the data.

You need to take a different approach to solve your problem: rather than sending the information forward to another view controller, send that data to a common place where it can be found by the other view controller once it is presented. This "common place" is usually referred to as the Model (in the Model-View-Controller sense). Make a singleton object with the queue of URLs, and call its addUrl: method from the first view controller. The second view controller could then call the getUrlQueue method to examine all queued URLs.

Illustration

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks for the answer but how would i go about continuously checking for a change with the url wanting to send from a different view – Tom Dec 17 '13 at 17:22
  • I just posted my own answer which was a solution and posted the code so I accepted that. – Tom Dec 20 '13 at 15:03
2

I figured it out and in then end i sent a url through the calling of a -(void) statement form another view.

For example:

In the secondviewconntroller.h file i would set a void statement...

-(void)addDownloadRequest:(NSString *)request;

This line of code means it can be called upon in other class and viewconntrollers.

Then in my secondviewconntroller.m file i would set the -(void) with the function i want to preform when it has been called so in my case i wanted to use the url to download so...

-(void)addDownloadRequest:(NSString *)request{
    NSLog(@"Called");
    NSString *URL = request;
    ///Download code here now i have the URL
}

The code above gets the sent URL in the form as a string called request and then sets the URL variable to the sent string called request.

In my first viewconntroller I then need to call the -(void) statement so I...

secondviewconntroller *theVIEW=[[secondviewconntroller alloc]init];
[theVIEW addDownloadRequest:@"The URL or STRING wanting to be sent"]; 

This code I place in an -(IBAction) which allowed me to send the variable or string when i called upon the action. This successfully allowed the download URL to be sent through class.

Thanks for all of your help.

Tom
  • 640
  • 1
  • 7
  • 25
  • This answer is quite wrong: you keep allocating a new instance of the `secondviewconntroller` each time you are adding a `URL`, so it is not possible to add a second URL to the same view controller: there's going to be a new instance created each time that you add a URL. Note that this is pretty much what I said in my first answer, now deleted, to which you replied that I " obviously did not read the question". – Sergey Kalinichenko Dec 20 '13 at 15:12
1

Define instance variables and a custom init method to set them. Something like this:

@implementation MyViewController {
  id data1;
  id data2;
}
-(id)initWithData1:(id)d1 data2:(id)d2 {
  self = [super init];
  if(self) {
    data1 = d1;
    data2 = d2;
  }
  return self;
}
@end

Obviously, remember to declare this initializer in the header too.

Linuxios
  • 34,849
  • 13
  • 91
  • 116