I have an uiImageview which is intended to use in multiple viewControllers.
What I am thinking is an independant UiImageview that can bind and
unbind from viewController to viewController if needed.
Here is my bind/ unbind part of code.
#synthesize uiImgView1;
-(void) sendUIObjToViewController : (UIImageView*)imgView : (UIViewController*)vController{
[vController.view addSubview:imgView];
}
-(UIImageView*)constructUiImgView : (NSString*) imgAddress{
UIImageView *imgView = [[UIImageView alloc] initWithFrame : CGRectMake(50,50,100,100)];
[imgView setImage:[UIImage imageNamed:imgAddress]];
return imgView;
}
-(void) construct : (UIViewController*) vController{
uiImgView1 = [self constructImgView : #"abc.png"];
[self sendUIObjToViewController : uiImgView1 : vController];
}
If I call addSubView method from scope of constructUiImgView,
addSubView works.
-(UIImageView*)constructUiImgView : (NSString*) imgAddress : (viewController*)vController{
UIImageView *imgView = [[UIImageView alloc] initWithFrame : CGRectMake(50,50,100,100)];
[imgView setImage:[UIImage imageNamed:imgAddress]];
[vController.view addSubView imgView];
return imgView;
}
However, that means constructed uiImageView is depandant to one
specific viewController and I don't want to do that.
and If I call sendUiObjToViewController method, uiImageView
should bind to view of viewController but not works.
Am I doing some mistake of referencing variable or something??
My knowledge of Object-c is very limited so I really
need you guys help. Could you tell me what is wrong with my code?
any help will be appreciated.
Related
I am currently trying to select a UIImageView that I have created in the storyboard in my view controller.
Is there a method equivalent to android's findViewbyId in objective c?
Why not just make an IBOutlet for the imageview?
Then you can just access it by something like
self.imageView
Where imageView is what you named your outlet.
Place a UIButton over your UIImageView.
After creating an IBOutlet as Samantha suggested you will need to add a gesture recogniser to your UIImageView.
I would suggest adding the gesture recognizer in viewDidLoad:
-(void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *singlePress = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewPressed)];
[singleTap setNumberOfTapsRequired:1];
[_imageView addGestureRecognizer:singleTap];
}
- (void)imageViewPressed {
//Perform imageView action here
}
Use the viewWithTag
UIImageView *yourImage = (UIImageView*)[self.view viewWithTag:20];
I use following code in VC1 :
ResultViewController *primaryView = [[ResultViewController alloc] initWithNibName:#"ResultView" bundle:nil];
UIImageView *profileView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"profile.png"]];
[_coinView setPrimaryView: primaryView.view];
[_coinView setSecondaryView: profileView];
[_coinView setSpinTime:1.0];
than I have following code in a View file:
-(IBAction) flipTouched:(id)sender{
[UIView transitionFromView:(displayingPrimary ? self.primaryView : self.secondaryView)
toView:(displayingPrimary ? self.secondaryView : self.primaryView)
duration: _spinTime
options: UIViewAnimationOptionTransitionFlipFromLeft+UIViewAnimationOptionCurveEaseInOut
completion:^(BOOL finished) {
if (finished) {
displayingPrimary = !displayingPrimary;
}
}
}];
}
this code combo helps me to flip a view shown in VC1, it works well, but now what I am trying to do is to have a button shown in VC1, when it clicked, I can also flip the same view. So I guess the functional code should be written in VC1, and I just cannot figure out a good code to do that, anyone can help me please? BTW, how can I call "[self loadData]" of VC1 in view file?
It sounds like you just need to wire up flipTouched: to a UIButton:
UIButton *myButton = [[UIButton alloc] init]; // Or an outlet from your XIB/storyboard
[myButton addTarget:self
action:#selector(flipTouched:)
forControlEvents:UIControlEventTouchUpInside];
[_coinView flipTouched: nil ];
I find out this works good for me.
I have a UIImageView in a storyboard, connected to the header of the subclassed UIViewController like this:
#property (retain, nonatomic) IBOutlet UIImageView *displayPhoto;
In the .m of the UIViewController subclass, I try to set the image of the displayPhoto.
#synthesize displayPhoto;
-(void)awakeFromNib {
UIImage *image = [UIImage imageNamed:#"dp.jpg"];
[displayPhoto setImage:image];
displayPhoto.layer.cornerRadius = displayPhoto.image.size.width/2;
[self.view addSubview:displayPhoto];
}
but it does absolutely nothing. If I add one more line, adding another view of the image like so: [self.view addSubview:[[UIImageView alloc] initWithImage:image]];, making my methodf
#synthesize displayPhoto;
-(void)awakeFromNib {
UIImage *image = [UIImage imageNamed:#"dp.jpg"];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
[displayPhoto setImage:image];
displayPhoto.layer.cornerRadius = displayPhoto.image.size.width/2;
[self.view addSubview:displayPhoto];
}
It suddenly starts working and adds to instances of the image to the view. One in the top left corner (from the line I added) and a second in the storyboard image location.
What's changing? How is adding that one line making my code work and is there any work around so I can make the image just show up as normal?
If you added the UIImage in your storyboard and connected the outlet correctly, there is no need to create a new image programmatically. Try this.
- (void) viewDidLoad {
[super viewDidLoad];
self.displayPhoto.image = [UIImage imageNamed:#"dp.jpg"];
}
Check the connection of imageview to storyboard will solve your problem.
Use this:
self.imgObj.image=[UIImage imageNamed:#"Your string"];
I'm fairly new to iOS programming and I have a question..
I'm in a situation right now where I want to have a set of buttons display an image based on the property "name" I have given them. There are three kinds of names and i want the buttons to display the image that suits the name.
So in my button class I have set up this method :
-(void) checkName {
if([self.name isEqualToString:#"v60"]){
[recipeImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"v60.png"]]];
}
else if([self.name isEqualToString:#"aeropress"]){
[recipeImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"aeropress.png"]]];
}
else if([self.name isEqualToString:#"chemex"]){
[recipeImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"chemex.png"]]];
}
}
In my recipeButton.h I have set up the property like this :
#property (strong) NSString *name;
and in my recipeButton.m I have synthesized it like this :
#synthesize name;
in my view controller I give my buttons the name property like this :
recipeButton *recipeButton1 = [[recipeButton alloc] init];
[recipeButton1 setTitle:#"Brewology V60" forState:UIControlStateNormal];
[recipeButton1 addTarget:self action:#selector(pushExample)forControlEvents:UIControlEventTouchUpInside];
recipeButton1.name =#"v60";
anybody help me? The images don't show and I think I'm missing something very important.
-(void) checkName
{
[recipeImage setBackgroundImage:[self getImage:#"name Property"] forState:UIControlStateNormal];
}
-(UIImage *)getImage:(NSString *)property
{
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png",property]];
return image;
}
I'm trying to pass a UIImage value to a UIImageView inside a View that I'm adding as a subView.
Here's what I'm doing in my mainViewController:
- (IBAction)selectStore:(id)sender {
StoreSelectorViewController *SSVC = [[StoreSelectorViewController alloc] initWithNibName:#"StoreSelectorViewController" bundle:nil];
[self.navigationController addChildViewController:SSVC];
[SSVC.backgroundView setImage:[UIImage imageNamed:#"CoolBG.png"]];
[self.view.window addSubview:SSVC.view];
}
I did the following in my StoreSelectorViewController.h file:
#property (nonatomic, retain) IBOutlet UIImageView *backgroundView;
And I also linked it in the XIB file.
Everything loads, except that I never receive the UIImage value. Thanks
Sometimes using this doesn't seem to work:
[SSVC.backgroundView setImage:[UIImage imageNamed:#"CoolBG.png"]];
So instead I use this two line version, in a slightly dif't order, setting the image, before displaying its parent (SSVC):
StoreSelectorViewController *SSVC = [[StoreSelectorViewController alloc] initWithNibName:#"StoreSelectorViewController" bundle:nil];
UIImage *img = [UIImage imageNamed:#"CoolBG.png"];
[SSVC.backgroundView setImage:img];
[self.navigationController addChildViewController:SSVC];
[self.view.window addSubview:SSVC.view];
You really shouldn't be calling addChildViewController regardless, it's not what you want. You're not making use of the UIViewController either, just stealing its view property. It's possible that because of this, the UIViewController is not fully instantiated since it's not presented (viewWill/Did) methods never get called, and so backgroundView is probably nil.
The problem is in this line [self.view.window addSubview:SSVC.view]; So just replace the line to below:-
Also you want to add imageview as a subview of view. So use below code where backgroundView is the imageview:-
[SSVC.view addSubview:SSVC.backgroundView];
[self.view addSubview:SSVC.view];.