Jump to content

Duvida Objective-C


HDiazz

Recommended Posts

Boas! Estou a iniciar-me na programação em Objective-C, a tentar ser um pouco auto-didacta e estava a tentar desenvolver uma aplicação que tem um TabBarController em que o utilizador pode escolher entre duas View's. Numas dessas View's tenho implementado um Navigation Controller. Essa View tem um botão (futuramente teria outros) que ao clicar abriria outra View (que contem uma UIImageView) e alteraria a imagem dessa UIImageView. O problema é que parece que não estou a conseguir fazê-lo,só consigo alterar a imagem se o fizer directamente no viewDidLoad da View que tem a UIImageView o que de todo não me daria jeito pois queria seleccionar a imagem consoante o botão que foi clicado.

View Controller com o Botão

#import "ViewController.h"
#import "ImageViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize botao;
@synthesize botao2;
//@synthesize imgHor;

-(IBAction)abrirImagem:(id)sender{

   ImageViewController *new = [[imageViewController alloc] initWithNibName:@"ImageViewController"                                     bundle:[NSBundle mainBundle]];
   UIImage *img = [uIImage imageNamed:@"Imagem.png"];
   [new.imgHor setImage:img];
   new.title = @"Ver Detalhe";
   [self.navigationController pushViewController:new animated:YES];

}

View Controller com UIImageView

#import "ImageViewController.h"

@interface ImageViewController ()

@end

@implementation ImageViewController

@synthesize imgHor;

Já pesquisei em tudo o que é sitio e não consegui ainda resolver isto,sei que pode ser um dúvida bastante básica,mas tenham em conta que iniciei-me nisto há 3 dias 😛 Obrigado desde já.

Edited by KTachyon
Formatar código
Link to comment
Share on other sites

Passa a UIImage para o controller:

-(IBAction)abrirImagem:(id)sender{

   ImageViewController *new = [[imageViewController alloc] initWithNibName:@"ImageViewController"                                     bundle:[NSBundle mainBundle]];
   new.theImg = [uIImage imageNamed:@"Imagem.png"];
   new.title = @"Ver Detalhe";
   [self.navigationController pushViewController:new animated:YES];

}

E depois no controlador fazes o que for necessário para colocar a imagem no UIImageView.

No teu caso, alterando o UIImageView directamente, tens que ter em conta que não é obrigatório que os outlets já estejam inicializados quando crias o controlador, pelo que o mais provável é estares a enviar mensagens para null.

Edited by KTachyon

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Desde já aproveito pra agradecer a resposta. Acho que já tinha experimentado isso e não tinha conseguido,mas voltei a fazê-lo e não deu mesmo,só se não estou a fazer alguma coisa bem daquilo que me disseste. Agora o método do botão ficou assim tal como me disseste:

-(IBAction)abrirImagem:(id)sender{

ImageViewController *new = [[imageViewController alloc] initWithNibName:@"ImageViewController" bundle:[NSBundle mainBundle]];
new.hor = [uIImage imageNamed:@"Imagem.jpg"];
new.title = @"Ver Detalhe";
[self.navigationController pushViewController:new animated:YES];

}

Aqui o atributo hor do Controller é uma UIImage.

E o Controller ficou:

@implementation ImageViewController

@synthesize imgHor;
@synthesize hor;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
       // Custom initialization
   }
   return self;
}

- (void)viewDidLoad
{
   [super viewDidLoad];
   self.imgHor.image = self.hor;
   // Do any additional setup after loading the view from its nib.
}

Não sei mesmo o que se passa :/

EDIT: Problema resolvido 😉 afinal o problema estava no ficheiro .xib,eu não tinha colocado a UIImageView como Outlet no File's Owner mas sim no Image View Controller. Quando alterei passou a dar da maneira que coloquei acima 😉 muito obrigado pela ajuda mesmo. Erro de iniciante lol

Edited by HDiazz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.