I've search the web a day for it but I can't find it:
How Can I change a UIImageView with a button in a another view on Xcode.
I'm new to Xcode.
Add this to your header file in the ImageView ViewController, and connect it:
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
Add to the other ViewController's .m file (don't forget to import your first file):
- (IBAction)btnPressed {
TheOtherViewController *vc = [[TheOtherViewController alloc] init];
vc.imageView.image = [UIImage imageNamed:#"MyImage"];
}
Related
I have made a window xib and cocoa class file (nswindowcontroller) and need to show the window from xib with an button action from main window.
In your main NSWindowController:
In the interface:
#property (nonatomic, strong) CustomWindowController *windowController;
In the implementation:
- (IBAction)didPressOpenWindowButton:(id)sender {
CustomWindowController *wc = [[CustomWindowController alloc] init];
[wc showWindow:nil];
[wc.window makeKeyAndOrderFront:nil];
_windowController = wc;
}
Connect your IBAction to your NSButton in the main window.
i have draged vertical slider in my ui and lable for to show the slider value when i move the slider.
i have written following ibaction method.but it is not working. could you please help me in solving the issue.
-(IBAction)sliderval:(id)sender{
NSSlider *slider=[[NSSlider alloc]init];
[slider setMinValue:50];
[slider setMaxValue:100];
[slider setTarget:self];
[slider setAction:#selector(sliderDidMove:)];
[_textField setValue:slider];
}
Drag and drop UISlider and UILabel to view in xib. connect IBOutlet to interface variables and IBAction with event 'Value Changed'.
#interface ViewController ()
{
__weak IBOutlet UISlider *slider;
__weak IBOutlet UILabel *sliderValueLbl;
}
#end
- (IBAction)sliderMoved:(id)sender {
sliderValueLbl.text = [NSString stringWithFormat:#"%f",slider.value];
}
I've searched this forum (and others) but haven't found a solution yet.
I have a app in landscape mode. Everythin fine, but when I change to another view (with a UITable) its in portrait mode and doesnt rotate. I have tried whole day but cant figure it out.
#property (nonatomic, strong) IBOutlet UIView *aView;
#property (nonatomic, strong) IBOutlet UIViewController *aViewcontroller;
-----
aViewcontroller = [[UIViewController alloc] initWithNibName:#"ViewController" bundle:nil];
aViewcontroller.view = aView;
-----
[self presentModalViewController:aViewcontroller animated:YES];
//got my view in portrait orientation.
My views in my .xib file are all set to Orientation:Landscape.
This should be simple, right?
Thanks beforehand!
By default UIViewController supports only Portrait orientation. In order to support other orientations you need create new custom view controller which supports the orientations you need.
Steps:
Create new custom view controller ViewControllerLandscape
Then import it to your mainn view controller
#import "ViewControllerLandscape.h"
Change your code:
aViewcontroller = [[ViewControllerLandscape alloc] initWithNibName:#"ViewController" bundle:nil];
aViewcontroller.view = aView;
[self presentModalViewController:aViewcontroller animated:YES];
in your ViewControllerLandscape add the method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
return YES;
}
return NO;
}
I'm having trouble figuring this one out. I have a custom UIControl class set up to hold a UIImage and UILabel, and my UITableViewCell class holds two of these UIControls (leftProduct, & rightProduct)
#interface FeaturedProductControl : UIControl
#property (strong, nonatomic) IBOutlet UIImageView *featuredProductPhoto;
#property (strong, nonatomic) IBOutlet UILabel *featuredProductDescription;
#property (strong, nonatomic) Product *featuredProduct;
- (id)initWithProduct:(Product *)product;
#end
#interface FeaturedTableCell : UITableViewCell
#property (strong, nonatomic) IBOutlet FeaturedProductControl *leftProduct;
#property (strong, nonatomic) IBOutlet FeaturedProductControl *rightProduct;
#end
The images and labels are being filled in using the init method during cellForRowAtIndexPath, and they're coming through just fine. I have a target action associated with the UIControls in the Storyboard, but the productClicked: method doesn't seem to be called. I've tried changing it out to add the target action programmatically, no luck.
However, if I add an alloc/init to the code, the productClicked: method triggers properly, but unfortunately the UILabel and UIPhoto now come up empty onscreen. Since the UIControls are designed in Storyboard, I think I'm not supposed to do the alloc calls myself, but the TableViewController doesn't seem to like that it's not being called. I've tried calling alloc within [[FeaturedTableCell alloc] init], but it had no effect.
Contents of cellForRowAtIndexPath:
cellIdentifier = #"Featured Row";
FeaturedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[FeaturedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
FeaturedRow *featuredRowData = [self.productIndexModel objectAtIndexPath:indexPath]; // This contains the necessary products to fill the Row
Product *leftProduct = [featuredRowData.skuList objectAtIndex:0];
cell.leftProduct = [[FeaturedProductControl alloc] initWithProduct:leftProduct]; // Actions trigger, but no data
// cell.leftProduct = [cell.leftProduct initWithProduct:leftProduct]; // Data is filled in, but no action
// [cell.leftProduct addTarget:self action:#selector(productClicked:) forControlEvents:UIControlEventTouchUpInside]; // the storyboard mapping works without this line of code
if (featuredRowData.skuList.count > 1)
{
Product *rightProduct = [featuredRowData.skuList objectAtIndex:1];
cell.rightProduct = [cell.rightProduct initWithProduct:rightProduct];
// cell.rightProduct = [[FeaturedProductControl alloc] initWithProduct:rightProduct]; // Yes, these two are reversed from the left side code above for testing
[cell.rightProduct addTarget:self action:#selector(productClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.rightProduct.hidden = NO; // right side column is hidden in case the number of products is odd
}
else
{
cell.rightProduct.hidden = YES;
}
[cell setNeedsLayout];
return cell;
Any idea what I'm doing wrong? I'm trying to keep as much of the initialization and setup as possible inside the storyboard, so I'd prefer not to go back to writing the whole UIControl programmatically.
Thanks everyone!
Figured it out. In my initWithProduct method, I included
self = [super init];
I commented out this line and the Touch Up Inside events started firing normally.
How do I put a template'd background image on a text area?
Not sure what you mean with "template'd"... but....
In interface builder, add the imageview behind the textview, uncheck the "opaque" box for the textview.
You can set the contents property of the text views layer.
#import <QuartzCore/QuartzCore.h>
...
- (void) viewDidLoad {
[super viewDidLoad];
textView.layer.contents = (id)[UIImage imageNamed:#"myImage.png"].CGImage
}
should work
If you want to add the image programmaticaly I managed to do it this way:
First in my textviewViewController.h I added an outlet for the UITextView.
#import <UIKit/UIKit.h>
#interface textviewViewController : UIViewController {
UITextView *textView;
}
#property (nonatomic, retain) IBOutlet UITextView *textView;
#end
Next, in my .xib I added my UITextView and connected my outlet.
Lastly in the viewDidLoad of textviewViewController.m I add the image as a subview to my textview.
- (void)viewDidLoad {
[super viewDidLoad];
// Change the pathForResource to reflect the name and type of your image file
NSString *path = [[NSBundle mainBundle] pathForResource:#"d" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.textView insertSubview:imgView atIndex:0];
[imgView release];
}
I tried the insertSubview:atIndex: with both 0 and 1 with the same result, but I would stick with 0 idicating that it's behind the textview.